Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logstash-logback event-specific custom fields (using StructuredArguments) are not added to JSON

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"
}
like image 588
bsiamionau Avatar asked Sep 20 '18 13:09

bsiamionau


1 Answers

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"
}
like image 124
bsiamionau Avatar answered Nov 10 '22 02:11

bsiamionau