I'm trying to add event-specific field to one of the logger statements using logstash documentation. My logger statement is following:
LOGGER.info("Executed REST request time={}ms", StructuredArguments.value("request_time_ms", elapsedTimeMs));
According to documentation:
StructuredArguments will be included in the JSON output if using LogstashEncoder/Layout or if using composite encoders/layouts with the arguments provider
That means StructuredArguments
should work fine for me as long as I'm using LogstashEncoder
:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
</encoder>
</appender>
But still request_time_ms
is missing in output 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"
}
It appears that StructuredArguments
won't be included in JSON even if you use LogstashEncoder
until you explicitly add ArgumentsJsonProvider
:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<provider class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>
</encoder>
</appender>
Now it works as expected:
{
"@timestamp": "2018-09-20T15:43:11.019+03:00",
"@version": 1,
"appname": "my_app",
"level": "INFO",
"level_value": 20000,
"logger_name": "com.example.MetricsAspect",
"message": "Executed REST request time=337ms",
"request_time_ms": 337,
"thread_name": "main"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With