Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Currently in spring boot 1.3, we could only log access log to a file in the filesystem. Is there any way to actually use the custom logger (like log4j2) to log the access log?

I am currently using undertow with spring boot, but after checking the spring boot source code, the undertow logger is initialized with DefaultAccessLogReceiver which is writing to file. I would like to use the AccessLogHandler if possible, and avoid writing a web filter which logs the access.

Is there any easy way around this? (except writing a pull request)

like image 743
Rowanto Avatar asked Feb 25 '16 00:02

Rowanto


People also ask

How do spring boots handle logs?

React Full Stack Web Development With Spring BootSpring Boot uses Apache Commons logging for all internal logging. Spring Boot's default configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. Using these, we can configure the console logging as well as file logging.

How do I create a log file in spring boot project?

To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.


1 Answers

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 DefaultAccessLogReceiver and 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
    }
}
like image 128
thirstycrow Avatar answered Sep 22 '22 08:09

thirstycrow