【发布时间】:2015-11-04 07:29:14
【问题描述】:
我有一个 Maven 项目,我想在其中启用 Spring 日志记录到日志文件。我使用的日志系统是 log4j。我已将它放在我的 log4j.properties 文件中,但这还不够:
log4j.category.org.springframework=ALL
我想将日志放到现有的附加程序中,或者放到单独的文件中。
【问题讨论】:
我有一个 Maven 项目,我想在其中启用 Spring 日志记录到日志文件。我使用的日志系统是 log4j。我已将它放在我的 log4j.properties 文件中,但这还不够:
log4j.category.org.springframework=ALL
我想将日志放到现有的附加程序中,或者放到单独的文件中。
【问题讨论】:
对于单独的文件,使用 appender 文件定义根记录器,如下所示:
# Define the root logger with appender file
log4j.rootLogger = info, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=/your/path/to/logfile/logfilename.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=info
# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true
# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=[%d{dd/MM/yyyy HH:mm:ss} %-5p %c{1}:%L] Method:- %M : %m%n
编辑
根据doc在属性文件中设置日志级别
log4j.level.org.springframework.web: info
【讨论】: