Duplicate log entries log4j

自古美人都是妖i 提交于 2019-12-08 16:53:28

问题


I am getting duplicate entries in my log file. Have attached my log4j.properties below.

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

Getting dulicate entries in log.log file configured above.

Log extract:

[ 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

Pls help


回答1:


If you turn off additivity, the loggers that are children of the parents won't cause double logging. For instance:

#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



回答2:


com.singaporeair.maps is a superset of com.singaporeair.maps.app.service.impl.gantt and com.singaporeair.maps.app.dao.impl.gantt

Everything that matches com.singaporeair.maps.app.dao.impl.gantt will also match com.singaporeair.maps which will result in 2 log entties.

Guess 1: You need to turn off appender inheritance. It appears that this is wrong.

Guess 2: The root logger and the com.singaporeair.maps are both logging to the SL and EL appenders. This is just a guess, but try changing this:

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

to this:

log4j.logger.com.singaporeair.maps=TRACE



回答3:


Probably would be helpful for those who experience duplicate problem in a multithread application (couldn't find the answer in google):

This happens when one thread is done and another thread open logger to the same log file which the first thread used to write.

.removeAllAppenders() before I added a new appender helped to resolve the issue.



来源:https://stackoverflow.com/questions/18489929/duplicate-log-entries-log4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!