spring boot tomcat access logs log4j2 elastic common schema

Viewed 355

Configuring spring boot to use log4j2 and the ECS was simple enough. All we needed to do was include the following dependencies in our pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
  <groupId>co.elastic.logging</groupId>
  <artifactId>log4j2-ecs-layout</artifactId>
  <version>1.0.1</version>
</dependency>

Then in the log4j2.xml file we just needed to include something like this:

<Console name="prod" target="SYSTEM_OUT">
  <EcsLayout serviceName="${service-name}" stackTraceAsArray="true" includeOrigin="true" includeMarkers="true" />
</Console>

However, when we configure spring boot to log the application access logs in application.yml we do this:

server:
  tomcat:
    accesslog:
      enabled: true
      encoding: UTF-8
      directory: /dev
      prefix: stdout
      buffered: false
      suffix:
      file-date-format:
      pattern: "ACCESS_LOG: %h %l %u \"%{yyyy-MM-dd HH:mm:ss.SSS}t\" \"%r\" status=%s size=%b \"user-agent=%{User-Agent}i\""

The access logs are not logged as json with the EcsLayout.

How can I configure a spring boot application to log tomcat access logs as json to the console using the elastic common schema (json) format?

0 Answers
Related