log4j自定义日志级别

时间:2016-04-21 17:22:47

标签: java logging

我有一个场景,我有一个单独的自定义级别定义XYZLogLevel用于日志记录,我有2个滚动文件appender,其中第二个appender专门保留用于记录来自XYZLogLevel的日志消息。但是,这两个文件中的日志都是不合需要的。

注意: -

  1. 日志不是特定于包的,因此不会为包添加可加性 工作
  2. 一切都必须通过log4j.properties文件完成。
  3. 将LevelRangeFilter添加到第一个appender会部分解析它,但是     当我为另一个自定义级别添加第三个appender时,我再次看到     第二和第三个appender重复。

    log4j.rootCategory=ERROR, F, XYZLOG, LMNLOG
    
    log4j.appender.F=org.apache.log4j.RollingFileAppender
    log4j.appender.F.File=f_log.log
    log4j.appender.F.MaxFileSize=5MB
    log4j.appender.F.MaxBackupIndex=10
    log4j.appender.F.layout = org.apache.log4j.PatternLayout
    log4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
    
    log4j.appender.XYZLOG=org.apache.log4j.RollingFileAppender
    log4j.appender.XYZLOG.File=xyz_reporting.log
    log4j.appender.XYZLOG.threshold=XYZLOG#com.services.domain.xyzlogs.XYZLogLevel
    log4j.appender.XYZLOG.filter.a=org.apache.log4j.varia.LevelMatchFilter    
    log4j.appender.XYZLOG.filter.a.LevelToMatch=XYZLOG#com.services.domain.xyzlogs.XYZLogLevel
    log4j.appender.XYZLOG.filter.a.AcceptOnMatch=true 
    log4j.appender.XYZLOG.MaxFileSize=100KB
    log4j.appender.XYZLOG.MaxBackupIndex=10
    log4j.appender.XYZLOG.layout=org.apache.log4j.PatternLayout
    log4j.appender.XYZLOG.layout.ConversionPattern=%m%n
    
    log4j.appender.LMNLOG=org.apache.log4j.RollingFileAppender
    log4j.appender.LMNLOG.File=lmn_reporting.log
    log4j.appender.LMNLOG.threshold=LMNLOG#com.services.domain.lmnlogs.LMNLogLevel
    log4j.appender.LMNLOG.filter.a=org.apache.log4j.varia.LevelMatchFilter    
    log4j.appender.LMNLOG.filter.a.LevelToMatch=LMNLOG#com.services.domain.lmnlogs.LMNLogLevel
    log4j.appender.LMNLOG.filter.a.AcceptOnMatch=true 
    log4j.appender.LMNLOG.MaxFileSize=100KB
    log4j.appender.LMNLOG.MaxBackupIndex=10
    log4j.appender.LMNLOG.layout=org.apache.log4j.PatternLayout
    log4j.appender.LMNLOG.layout.ConversionPattern=%m%n
    

    也许这可以通过org.apache.log4j.varia.DenyAllFilter来解决,但不知怎的,我无法为属性文件配置它。我相信该功能仅适用于XML配置。

    这是一个非常棘手的情况,因为我没有自由切换到XML配置,任何有关这方面的帮助都会受到赞赏。 链接和示例会很棒,因为我很快就会理解它们。

1 个答案:

答案 0 :(得分:0)

mysqli

log4j.appender.LMNLOGAPPENDER.threshold = LMNLOG#com.services.domain.lmnlogs.LMNLogLevel
    log4j.appender.LMNLOGAPPENDER.MaxFileSize = 100KB     log4j.appender.LMNLOGAPPENDER.MaxBackupIndex = 10     log4j.appender.LMNLOGAPPENDER.layout = org.apache.log4j.PatternLayout     log4j.appender.LMNLOGAPPENDER.layout.ConversionPattern =%米%N

I am answering my own question.

First you need to create a logger and set additivity as FALSE.

log4j.logger.XZYLOG=XZYLOG#XYZLOG#com.services.domain.xyzlogs.XYZLogLevel, XYZAPPENDER

log4j.additivity.XZYLOG=false

log4j.logger.LMNLOG=LMNLOG#XYZLOG#com.services.domain.lmnlogs.LMNLogLevel, LMNAPPENDER

log4j.additivity.LMNLOG=false

Then configure the appenders in following way.

log4j.appender.XYZLOGAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.XYZLOGAPPENDER.File=xyz_reporting.log
log4j.appender.XYZLOGAPPENDER.threshold=XYZLOG#com.services.domain.xyzlogs.XYZLogLevel
log4j.appender.XYZLOGAPPENDER.MaxFileSize=100KB
log4j.appender.XYZLOGAPPENDER.MaxBackupIndex=10
log4j.appender.XYZLOGAPPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.XYZLOGAPPENDER.layout.ConversionPattern=%m%n

log4j.appender.LMNLOGAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.LMNLOGAPPENDER.File=lmn_reporting.log