我正在努力使记录器在CommandLineRunner中写入文件。日志在SpringApplication.run之前写入,但之后仅进入控制台。
@SpringBootApplication
@Log4j2
public class B4bApplication {
public static void main(String[] args) {
log.info("start");
SpringApplication.run(B4bApplication.class, args);
log.info("end"); // this comes only in console
}
}
@Component
@Log4j2
public class B4bProcess implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("Start B4B"); // This comes only in console
}
}
从调试日志中,我看到日志文件已关闭:
DEBUG StatusLogger Started configuration XmlConfiguration[...springframework/boot/logging/log4j2/log4j2.xml] OK.
TRACE StatusLogger Stopping XmlConfiguration[location=C:\git\b4b\src\resources\log4j2.xml]...
TRACE StatusLogger XmlConfiguration notified 3 ReliabilityStrategies that config will be stopped.
我的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="logDir">logs</Property>
<Property name="loggingPattern">%d{HH:mm:ss} %-5p %c{1} - %m%n</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="${loggingPattern}"/>
</Console>
<File name="general" fileName="${logDir}/general_${sys:COUNTRY_CODE}_${date:yyyyMMdd}.log">
<PatternLayout pattern="${loggingPattern}"/>
</File>
</Appenders>
<Loggers>
<Logger name="com.swedbank" level="${env:INSURANCE_LOG_LEVEL:-info}" additivity="true">
<AppenderRef ref="general" level="info"/>
<AppenderRef ref="STDOUT" level="info"/>
</Logger>
<Root level="DEBUG">
<AppenderRef ref="general"/>
</Root>
</Loggers>
</Configuration>