我在日志文件中收到重复的条目。 已经附加了我的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
请帮忙
答案 0 :(得分:10)
如果关闭可加性,则作为父项子项的记录器不会导致双重记录。例如:
#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
答案 1 :(得分:1)
com.singaporeair.maps
是com.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 appender。这只是一个猜测,但尝试改变这个:
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
到此:
log4j.logger.com.singaporeair.maps=TRACE
答案 2 :(得分:1)
对于那些在多线程应用程序中遇到重复问题的人(在谷歌找不到答案)可能会有所帮助:
当一个线程完成并且另一个线程打开记录器到第一个线程用来写入的同一个日志文件时,就会发生这种情况。
在添加新的appender之前, .removeAllAppenders()
有助于解决问题。