11

我的日志文件中出现重复条目​​。在下面附上了我的 log4j.properties。

log4j.properties:

###############################################################################
# log4j Configuration file: Defines following loggers
# SL    -   Standard root Logger
# EL    -   Error Logger with the threshold level explicitly set to ERROR
# DL    -   Data base logger - to log db queries separately
# BL    -   Batch logger
###############################################################################

log4j.rootLogger=TRACE,SL,EL
log4j.rootLogger.additivity=false

#Standard Log
log4j.appender.SL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.SL.File=${log.file}/log.log
log4j.appender.SL.layout=org.apache.log4j.PatternLayout
log4j.appender.SL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n

#Error Log
log4j.appender.EL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.EL.File=${log.file}/error.log
log4j.appender.EL.layout=org.apache.log4j.PatternLayout
log4j.appender.EL.Threshold=ERROR
log4j.appender.EL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n

# Database Log
log4j.logger.org.springframework.jdbc=DEBUG,DL

log4j.appender.DL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DL.File=${log.file}/db.log
log4j.appender.DL.layout=org.apache.log4j.PatternLayout
log4j.appender.DL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n

#Forecast Log
log4j.appender.MAPS_FC=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MAPS_FC.File=${log.file}/forecast.log
log4j.appender.MAPS_FC.layout=org.apache.log4j.PatternLayout
log4j.appender.MAPS_FC.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n

#Logger configuration
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
log4j.logger.com.singaporeair.maps.app.service.impl.gantt=DEBUG,MAPS_FC
log4j.logger.com.singaporeair.maps.app.dao.impl.gantt=DEBUG,MAPS_FC

在上面配置的log.log文件中获取重复条目。

日志提取:

[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:69) Processing Time(ms): BaseDAOImpl: getBatchDetails: 63
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:69) Processing Time(ms): BaseDAOImpl: getBatchDetails: 63
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:71) BaseDAOImpl: getBatchDetails: OUT
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:71) BaseDAOImpl: getBatchDetails: OUT

请帮忙

4

3 回答 3

15

如果您关闭可加性,则作为父母子女的记录器不会导致双重记录。例如:

#Logger configuration
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
log4j.additivity.com.singaporeair.maps=false

log4j.logger.com.singaporeair.maps.app.service.impl.gantt=DEBUG,MAPS_FC
log4j.additivity.com.singaporeair.maps.app.service.impl.gantt=false

log4j.logger.com.singaporeair.maps.app.dao.impl.gantt=DEBUG,MAPS_FC
log4j.additivity.com.singaporeair.maps.app.dao.impl.gantt=false
于 2013-08-28T16:23:19.177 回答
1

com.singaporeair.mapscom.singaporeair.maps.app.service.impl.gantt和的超集com.singaporeair.maps.app.dao.impl.gantt

匹配的所有com.singaporeair.maps.app.dao.impl.gantt内容也将匹配com.singaporeair.maps,这将导致 2 个日志实体。

猜想1:需要关闭appender继承。看来这是错误的。

猜测 2:根记录器和 com.singaporeair.maps 都记录到 SL 和 EL 附加程序。这只是一个猜测,但请尝试更改:

log4j.logger.com.singaporeair.maps=TRACE,SL,EL

对此:

log4j.logger.com.singaporeair.maps=TRACE
于 2013-08-28T13:59:54.637 回答
1

对于那些在多线程应用程序中遇到重复问题的人可能会有所帮助(在谷歌中找不到答案):

当一个线程完成并且另一个线程将记录器打开到第一个线程用来写入的同一日志文件时,就会发生这种情况。

.removeAllAppenders()在我添加新的附加程序之前帮助解决了这个问题。

于 2015-09-27T10:39:13.543 回答