【发布时间】:2019-11-22 05:51:16
【问题描述】:
在 2.2.1 RELEASE 中开发一个 spring boot 应用程序。除了使用 log4j.properties 进行日志记录外,一切都运行良好。
在apoplication.properies 中,添加如下所示的logging.config
logging.config =${external.config}/log4j.properties.
Pom.xml 文件,排除spring-boot-starter-logging 并添加spring-boot-starter-log4j,如下所示
<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>
log4j.properties
log4j.rootLogger=error,Service
# Direct log messages to a log file
log4j.appender.Service=org.apache.log4j.RollingFileAppender
log4j.appender.Service.File=C:/log/Service.log
log4j.appender.Service.MaxFileSize=1MB
log4j.appender.Service.MaxBackupIndex=1
log4j.appender.Service.layout=org.apache.log4j.PatternLayout
log4j.appender.Service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - [%X{AUDIT_USER}] %m%n
我在堆栈中引用了以下链接
1.Logger not printing with log4j.properties within Spring Boot 1.5.7
2.Spring boot logging with log4.properties file in not working
编辑 1
作为Andy Wilkinson cmets,我修改了log4j.properties的文件内容和文件名,文件名应该是log4j2.properties。
我尝试将一些字符串记录到日志文件中,如下所示
public class ServiceMain {
private static final Logger logger = LogManager.getLogger(ServiceMain.class);
public static void main(String[] args) {
SpringApplication.run(ServiceMain.class, args);
logger.debug("----------------------Stating spring booot----------------------");
}
}
调试日志"----------------------Stating spring booot----------------------" 不会写入c:/log/service.log
日志文件
2019-11-22 05:20:14,631 INFO o.s.b.StartupInfoLogger [main] Starting ServiceMain v7.0.0.0 on host-4 with PID 119176 (D:\Service\target\Service-1.0.jar started by Administrator in D:\Service\target)
2019-11-22 05:20:14,634 DEBUG o.s.b.StartupInfoLogger [main] Running with Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE
2019-11-22 05:20:14,635 INFO o.s.b.SpringApplication [main] The following profiles are active: Service
2019-11-22 05:20:24,898 INFO o.s.b.StartupInfoLogger [main] Started ServiceMain in 12.312 seconds (JVM running for 14.189)
我错过了任何log4j2.properties 配置吗?
【问题讨论】:
标签: spring spring-boot log4j