Java 重复的日志条目 log4j

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18489929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:06:59  来源:igfitidea点击:

Duplicate log entries log4j

javaspring-mvclog4j

提问by prabu

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

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

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

Getting dulicate entries in log.logfile configured above.

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

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

请帮忙

回答by DwB

com.singaporeair.mapsis a superset of com.singaporeair.maps.app.service.impl.ganttand com.singaporeair.maps.app.dao.impl.gantt

com.singaporeair.maps是的超集com.singaporeair.maps.app.service.impl.ganttcom.singaporeair.maps.app.dao.impl.gantt

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

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

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

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

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:

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

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

to this:

对此:

log4j.logger.com.singaporeair.maps=TRACE

回答by amcintosh

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

回答by DenisD

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

可能对那些在多线程应用程序中遇到重复问题的人有所帮助(在 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.

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