How to use custom logger to log access log in spring boot How to use custom logger to log access log in spring boot spring spring

How to use custom logger to log access log in spring boot


A trick for this kind of hard-coded-thus-not-customizeable problem is to hide the class to kick out with a new one with the same package and name. All you have to do is to provide a log4j based DefaultAccessLogReceiverand make sure it can be search by the classloader before the one in the undertow library.

package io.undertow.server.handlers.accesslog;public class DefaultAccessLogReceiver implements AccessLogReceiver {    public void logMessage(final String message) {        // TODO: log with log4j    }}


Spring Boot has no mandatory logging dependency, except for the commons-logging API, of which there are many implementations to choose from. To use Logback you need to include it, and some bindings for commons-logging on the classpath. The simplest way to do that is through the starter poms which all depend on spring-boot-starter-logging. For a web application you only need spring-boot-starter-web since it depends transitively on the logging starter. For example, using Maven:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency>

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath. If Logback is available it is the first choice.

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead. If you aren’t using the starter poms then you need to provide commons-logging (at least) in addition to your chosen version of Log4j.

The simplest path is probably through the starter poms, even though it requires some jiggling with excludes, .e.g. in Maven:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><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-log4j</artifactId></dependency>

To use Log4j 2, simply depend on spring-boot-starter-log4j2 rather than spring-boot-starter-log4j.