logstash-logback-encoder

Accessing Request Header keys in logstash-logback in Spring Boot encoding in JSON format


My use case is that I need to print access logs of tomcat server in spring boot application. Logs should be in JSON format with custom field names. Like this:

{
  "_type": "access",
  "_timestamp": "2020-12-14 09:04:51.124",
  "_url":"/api/v2/activities/search",
  "_method": "GET",
  "_response_code": 200,
  "_processing_time_in_ms":0.004,
  "referer":"https://google.com",
  "user_agent":"XYZ",
  "_level": "info
}

Now I am using net.logstash.logback.encoder.AccessEventCompositeJsonEncoder encoder in logback.xml appending to console.

I have successfully got the values of other fields such as _response_code : "%statsCode by doing this.

But ,How can I access referer from request_headers object directly here in my custom json object?


Solution

  • Use logstash-logback-encoder's pattern provider along with logback's conversion word for request headers (%i or %header).

    The example given in the logstash-logback-encoder's README.md (linked above) shows how to do exactly that. Copied here for reference...

    <encoder class="net.logstash.logback.encoder.AccessEventCompositeJsonEncoder">
      <providers>
        <pattern>
          <pattern>
            {
            "custom_constant": "123",
            "tags": ["one", "two"],
            "remote_ip": "%a",
            "status_code": "%s",
            "elapsed_time": "%D",
            "user_agent": "%i{User-Agent}",
            "accept": "%i{Accept}",
            "referer": "%i{Referer}",
            "session": "%requestCookie{JSESSIONID}",
    ...
            }
          </pattern>
        </pattern>
      </providers>
    </encoder>