猿问

Logstash-logback 事件特定的自定义字段

我正在尝试使用logstash 文档将特定于事件的字段添加到记录器语句之一。我的记录器声明如下:


LOGGER.info("Executed REST request time={}ms", StructuredArguments.value("request_time_ms", elapsedTimeMs));

根据文档:


如果使用 LogstashEncoder /Layout 或使用复合编码器/布局和参数提供程序,则StructuredArguments 将包含在 JSON 输出中


这意味着StructuredArguments只要我在使用,就应该对我有用LogstashEncoder:


<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

    <encoder class="net.logstash.logback.encoder.LogstashEncoder">

    </encoder>

</appender>

但request_time_ms在输出 JSON 中仍然缺失:


{

    "@timestamp": "2018-09-20T14:00:43.560+03:00",

    "@version": 1,

    "appname": "my_app",

    "level": "INFO",

    "level_value": 20000,

    "logger_name": "com.example.MetricsAspect",

    "message": "Executed REST request time=258ms",

    "thread_name": "main"

}


智慧大石
浏览 516回答 1
1回答

开满天机

StructuredArguments即使您LogstashEncoder在明确添加之前使用,它似乎也不会包含在 JSON 中ArgumentsJsonProvider:<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">&nbsp; &nbsp; <encoder class="net.logstash.logback.encoder.LogstashEncoder">&nbsp; &nbsp; &nbsp; &nbsp; <provider class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>&nbsp; &nbsp; </encoder></appender>现在它按预期工作:{&nbsp; &nbsp; "@timestamp": "2018-09-20T15:43:11.019+03:00",&nbsp; &nbsp; "@version": 1,&nbsp; &nbsp; "appname": "my_app",&nbsp; &nbsp; "level": "INFO",&nbsp; &nbsp; "level_value": 20000,&nbsp; &nbsp; "logger_name": "com.example.MetricsAspect",&nbsp; &nbsp; "message": "Executed REST request time=337ms",&nbsp; &nbsp; "request_time_ms": 337,&nbsp; &nbsp; "thread_name": "main"}
随时随地看视频慕课网APP

相关分类

Java
我要回答