log4j (1.2.15) logging to different DailyRollingFileAppender log files for different log levels from same package?

huangapple go评论71阅读模式
英文:

log4j (1.2.15) logging to different DailyRollingFileAppender log files for different log levels from same package?

问题

我正在尝试为同一个包的不同日志级别(具体为INFO和ERROR)将日志写入2个不同的DailyRollingFileAppender日志文件。以下是我的log4j.properties文件:

log4j.rootLogger=INFO,webAppDRFA

log4j.category.com.webapp.controller.sd=INFO,sdInfoDRFA
log4j.category.com.webapp.controller.sd=ERROR,sdErrorDRFA
log4j.additivity.com.webapp.controller.sd=false

log4j.category.com.webapp.controller.td=INFO,tdInfoDRFA
log4j.category.com.webapp.controller.td=ERROR,tdErrorDRFA
log4j.additivity.com.webapp.controller.td=false

log4j.appender.webAppDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.webAppDRFA.DatePattern='.dd-MMM-yyyy.log'
log4j.appender.webAppDRFA.File=D:\WebApp-Logs\CMWebappLogs\webapp.log
log4j.appender.webAppDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.webAppDRFA.layout.ConversionPattern=%-5p[%d : Class-%C : Method-%M]: %m%n
log4j.appender.webAppDRFA.Threshold=WARN

#--[START] Info & Error DailyRollingFileAppender logging config for SD--
log4j.appender.sdInfoDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.sdInfoDRFA.DatePattern='_MM-DD-YYYY.log'
log4j.appender.sdInfoDRFA.File=D:\CMWebappLogs\SD\Info_Log\SD_Info.log
log4j.appender.sdInfoDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.sdInfoDRFA.layout.ConversionPattern=%-5p[%d : Class-%C : Method-%M]: %m%n
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.levelMin=INFO
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.levelMax=INFO
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.acceptOnMatch=true

log4j.appender.sdErrorDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.sdErrorDRFA.DatePattern='_MM-DD-YYYY.log'
log4j.appender.sdErrorDRFA.File=D:\CMWebappLogs\SD\Error_Log\SD_Error.log
log4j.appender.sdErrorDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.sdErrorDRFA.layout.ConversionPattern=%-5p[%d : Class-%C : Method-%M]: %m%n
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.levelMin=ERROR
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.levelMax=ERROR
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.acceptOnMatch=true
#--[END] Info & Error DailyRollingFileAppender logging config for SD--

#--[START] Info & Error DailyRollingFileAppender logging config for TD--
log4j.appender.tdInfoDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.tdInfoDRFA.DatePattern='_MM-DD-YYYY.log'
log4j.appender.tdInfoDRFA.File=D:\CMWebappLogs\TD\Info_Log\TD_Info.log
log4j.appender.tdInfoDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.tdInfoDRFA.layout.ConversionPattern=%-5p[%d : Class-%C : Method-%M]: %m%n
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.levelMin=INFO
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.levelMax=INFO
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.acceptOnMatch=true

log4j.appender.tdErrorDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.tdErrorDRFA.DatePattern='_MM-DD-YYYY.log'
log4j.appender.tdErrorDRFA.File=D:\CMWebappLogs\TD\Error_Log\TD_Error.log
log4j.appender.tdErrorDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.tdErrorDRFA.layout.ConversionPattern=%-5p[%d : Class-%C : Method-%M]: %m%n
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.levelMin=ERROR
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.levelMax=ERROR
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.acceptOnMatch=true
#--[END] Info & Error DailyRollingFileAppender logging config for TD--

上述配置仅生成Error_log文件,而Info_log文件根本不创建。此外,创建的Error_log文件不会滚动到下一个日期。是否可以实现上述要求?如果可以,请建议需要进行的更正。

英文:

I'm trying to write logs to 2 different DailyRollingFileAppender log files for different log levels (specifically INFO & ERROR) from the same package. Following is my log4j.properties file :

log4j.rootLogger=INFO,webAppDRFA

log4j.category.com.webapp.controller.sd=INFO,sdInfoDRFA
log4j.category.com.webapp.controller.sd=ERROR,sdErrorDRFA
log4j.additivity.com.webapp.controller.sd=false

log4j.category.com.webapp.controller.td=INFO,tdInfoDRFA
log4j.category.com.webapp.controller.td=ERROR,tdErrorDRFA
log4j.additivity.com.webapp.controller.td=false

log4j.appender.webAppDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.webAppDRFA.DatePattern='.'dd-MMM-yyyy'.log'
log4j.appender.webAppDRFA.File=D:\\WebApp-Logs\\CMWebappLogs\\webapp.log
log4j.appender.webAppDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.webAppDRFA.layout.ConversionPattern=%-5p[%d \: Class-%C \: Method-%M]\: %m%n
log4j.appender.webAppDRFA.Threshold=WARN

#--[START] Info & Error DailyRollingFileAppender logging config for SD--
log4j.appender.sdInfoDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.sdInfoDRFA.DatePattern='_'MM-DD-YYYY'.log'
log4j.appender.sdInfoDRFA.File=D:\\CMWebappLogs\\SD\\Info_Log\\SD_Info.log
log4j.appender.sdInfoDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.sdInfoDRFA.layout.ConversionPattern=%-5p[%d \: Class-%C \: Method-%M]\: %m%n
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.levelMin=INFO
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.levelMax=INFO
log4j.appender.sdInfoDRFA.filter.lrf4SDJIDRFA.acceptOnMatch=true

log4j.appender.sdErrorDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.sdErrorDRFA.DatePattern='_'MM-DD-YYYY'.log'
log4j.appender.sdErrorDRFA.File=D:\\CMWebappLogs\\SD\\Error_Log\\SD_Error.log
log4j.appender.sdErrorDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.sdErrorDRFA.layout.ConversionPattern=%-5p[%d \: Class-%C \: Method-%M]\: %m%n
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.levelMin=ERROR
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.levelMax=ERROR
log4j.appender.sdErrorDRFA.filter.lrf4SDJEDRFA.acceptOnMatch=true
#--[END] Info & Error DailyRollingFileAppender logging config for SD--

#--[START] Info & Error DailyRollingFileAppender logging config for TD--
log4j.appender.tdInfoDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.tdInfoDRFA.DatePattern='_'MM-DD-YYYY'.log'
log4j.appender.tdInfoDRFA.File=D:\\CMWebappLogs\\TD\\Info_Log\\TD_Info.log
log4j.appender.tdInfoDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.tdInfoDRFA.layout.ConversionPattern=%-5p[%d \: Class-%C \: Method-%M]\: %m%n
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.levelMin=INFO
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.levelMax=INFO
log4j.appender.tdInfoDRFA.filter.lrf4PAJIDRFA.acceptOnMatch=true

log4j.appender.tdErrorDRFA=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.tdErrorDRFA.DatePattern='_'MM-DD-YYYY'.log'
log4j.appender.tdErrorDRFA.File=D:\\CMWebappLogs\\TD\\Error_Log\\TD_Error.log
log4j.appender.tdErrorDRFA.layout=org.apache.log4j.PatternLayout
log4j.appender.tdErrorDRFA.layout.ConversionPattern=%-5p[%d \: Class-%C \: Method-%M]\: %m%n
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.levelMin=ERROR
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.levelMax=ERROR
log4j.appender.tdErrorDRFA.filter.lrf4PAJEDRFA.acceptOnMatch=true
#--[END] Info & Error DailyRollingFileAppender logging config for TD--

The above configuration generates only the Error_log files and Info_log files are not created at all. Also, the Error_log files created do not get rolled over to next date. Can the above requirement be achieved at all? If yes, please suggest me the corrections to be done.

答案1

得分: 2

问题在于你正在覆盖category配置。

你需要按照以下方式定义每个类别:

log4j.category.com.webapp.controller.sd=INFO,sdInfoDRFA,sdErrorDRFA

你必须定义类别发出日志跟踪所需的最低日志级别,对于你的情况,是INFO,以及类别将使用的不同appender。

过滤机制将会将不同的发出的日志跟踪路由到相应的appender和日志文件。

英文:

The problem is that you are overwriting the category configuration.

You need to define each category as follows:

log4j.category.com.webapp.controller.sd=INFO,sdInfoDRFA,sdErrorDRFA

You must define the minimum log level required for the category to emit log traces, in your case, INFO, and the different appenders that the category will use.

The filtering mechanism will route the different emitted traces to the corresponding appenders and logs files.

huangapple
  • 本文由 发表于 2020年8月3日 19:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63228610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定