简体   繁体   中英

Save logs to file with Spring Boot. Issue: Log4j do not log to file when I use @Slf4j

I use Slf4j with the spring boot project. I managed log4j.properties, and have logs in the console, but can't implement logging to the file.

log4j.properties :

# Root logger option
log4j.rootLogger=INFO, ERROR, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${user.dir}/Log4j/log4j-test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-4p] %d{dd-MM-yy HH:mm:ss} %t | %m%n

log4j2.formatMsgNoLookups=true
com.sun.jndi.rmi.object.trustURLCodebase=false
com.sun.jndi.cosnaming.object.trustURLCodebase=false

Also, I tried such options:

log4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=Log4j/log4j-test.log
log4j.appender.NotConsole.maxFileSize=20MB

Logging example:

@Slf4j
public class A{
    public void m(){
        log.info("log");
    }
}
  • Dependency supplied with org.springframework.boot:spring-boot-starter-logging:jar:2.6.6:

In any case, I haven't a file in the result.

How to correctly create logging to the file with this solution?

I found a solution:

1.

First of all, we need to exclude logback from the dependencies. Lockback is included in the spring-boot-starter-web and we can do it with gradle in the following way:

    implementation ("org.springframework.boot:spring-boot-starter-web:${spring_boot_version}"){
        exclude group:"org.apache.logging.log4j", module:"log4j-to-slf4j"
    }

In my case, the logback also supplied with spring-boot-starter-validation and in some reason gradle doesn't want to exclude logback from the both dependenisies. The working solution in this case is adding exclusion in the configurations block:

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

I added spring-boot-starter-log4j2 dependency:

    implementation "org.springframework.boot:spring-boot-starter-log4j2"

I created log4j2-spring.xml file in resources directory:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>

        <RollingFile name="RollingFile"
                     fileName="./logs/spring-boot-logger-log4j2.log"
                     filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <!-- rollover on startup, daily and when the file reaches
                    10 MegaBytes -->
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy
                        size="10 MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>

        <!-- LOG "com.proxy*" at TRACE level -->
        <Logger name="com.proxy" level="trace"></Logger>
    </Loggers>

</Configuration>

PS. This solution tested with spring_boot_version = '2.6.6'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM