Spring Boot和Log4j问题

时间:2015-09-25 21:36:38

标签: spring tomcat spring-boot log4j

我的log4j设置如下。当我作为Boot App运行时,日志正确地写在console,debug.log和dump.log中。下面是我在程序中编写登录console和debug.log

的方法
static final Logger LOG = Logger.getLogger(EnvironmentLoader.class);    
LOG.info("blah blah!");

以下是我在程序中编写的用于在dump.log中写入日志的内容

private static final Logger DUMP_LOG = Logger.getLogger("dumpLogger");
DUMP_LOG.info("blah blah!");

如果我作为Spring Boot App运行,两者都可以。如果我将其打包为war并在tomcat中运行,DUMP_LOG在dump.log中正确写入,但LOG未在console或debug.log中写入。我想知道为什么。

log4j.rootLogger=INFO, stdout, debugLog

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.appender.stdout.Target=System.out

log4j.appender.debugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugLog.Append=true
log4j.appender.debugLog.DatePattern='.'dd-MM-yyyy
log4j.appender.debugLog.File=${catalina.base}/logs/debug.log
log4j.appender.debugLog.MaxFileSize=10MB
log4j.appender.debugLog.encoding=UTF-8
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n

log4j.category.debugLogger=DEBUG, debugLog
log4j.additivity.debugLogger=false

log4j.appender.dumpLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dumpLog.Append=true
log4j.appender.dumpLog.DatePattern='.'dd-MM-yyyy
log4j.appender.dumpLog.File=${catalina.base}/logs/dump.log
log4j.appender.dumpLog.MaxFileSize=10MB
log4j.appender.dumpLog.encoding=UTF-8
log4j.appender.dumpLog.layout=org.apache.log4j.PatternLayout
log4j.appender.dumpLog.layout.ConversionPattern=%d - %m%n

log4j.category.dumpLogger=DEBUG, dumpLog
log4j.additivity.dumpLogger=false

1 个答案:

答案 0 :(得分:0)

以下依赖项解决了该问题。

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