【问题标题】:Avoiding Multiple Logging of Same Message Due to Default behaviour in Log4j由于 Log4j 中的默认行为,避免多次记录同一消息
【发布时间】:2014-09-19 14:04:11
【问题描述】:

我正在使用 Grails 应用程序并希望在不同的文件中记录消息。我想在不同的文件中记录异常、正常和 API 日志。但是根据 Log4j 的一般角色,如果我们将记录器级别设置为“信息”,那么警告和错误消息也将开始记录在这个文件中,而我想在不同的文件中记录错误消息。因此,因此我的错误消息将在错误文件和信息文件中记录两次。虽然我希望“信息”记录器只记录“信息”级别的消息而不是“错误”级别的消息。而“错误”记录器只记录错误消息。

以下是我的 Log4j 配置:

log4j = {
def layoutPattern = new PatternLayout("[%d{ISO8601}] %m \r\n")
def dailyRollingInfoFile = new DailyRollingFileAppender(
        name:"rollingInfoFileAppender",
        layout: layoutPattern,
        //Path of the Log File
        fileName:"C:\\MS-Logs\\Application\\MSLogs.log",
        datePattern: "'.'dd-MM-yyyy")

def dailyRollingExceptionFile = new DailyRollingFileAppender(
        name:"rollingExceptionFileAppender",
        layout: layoutPattern,
        //Path of the Log File
        fileName:"C:\\MS-Logs\\Exceptions\\ExceptionLogs.log",
        datePattern: "'.'dd-MM-yyyy")

def dailyRollingExceptionAPIFile = new DailyRollingFileAppender(
        name:"rollingAPIFileAppender",
        layout: layoutPattern,
        //Path of the Log File
        fileName:"C:\\MS-Logs\\API\\MS-NotificationsLogs.log",
        datePattern: "'.'dd-MM-yyyy")

 //For logging exceptions stack trace
appenders {
    appender dailyRollingInfoFile
    appender dailyRollingExceptionFile
    appender dailyRollingExceptionAPIFile
   }

root {
    info  'rollingInfoFileAppender', additivity: false
    debug 'rollingAPIFileAppender', additivity: false
    error 'rollingExceptionFileAppender'
}
}

现在,这就是我添加过滤器的方式:

dailyRollingExceptionFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'ERROR', acceptOnMatch: true))
dailyRollingExceptionFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())

//To make it sure that It will just Log, Messages by Info Logger
dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'INFO', acceptOnMatch: true))
dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())

//To make it sure that It will just Log, Messages by API Logger
dailyRollingAPIFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'DEBUG', acceptOnMatch: true))
dailyRollingAPIFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())

如何,有可能避免相同的消息在不同的文件中被记录两次?我们如何在不同的文件中记录消息而不在其他文件中重复?

感谢您的宝贵时间:)

【问题讨论】:

    标签: java grails logging log4j


    【解决方案1】:

    我相信 Log4j 的 LevelMatchFilter 可以让你做你想做的事:

    def dailyRollingInfoFile = new DailyRollingFileAppender(...)
    dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'INFO', acceptOnMatch: true))
    dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())
    

    DenyAllFilter 丢弃LevelMatchFilter 不匹配的消息(即除级别 INFO 之外的所有消息)

    【讨论】:

    • 感谢@Andrew,工作就像一个魅力
    • 你能否让我明白加法属性的作用是什么?
    • 加性与父/子记录器有关,而不是与日志级别有关。 log4j 手册提供了详细信息:logging.apache.org/log4j/2.x/manual/…
    • 好的...请查看我编辑的问题,在那里我添加了如何添加过滤器来过滤我的消息。但是在那之后,当我记录我的消息时,只有我通过'logger.error()'记录的消息被正确记录,而当我使用'logger.debug()'或'logger.info时没有mesg记录在他们的相应文件中()'
    • 我已经通过两种方式对此进行了测试:1.首先我使用了单个记录器:public static Logger logger = Logger.getLogger(this);记录所有消息(信息、调试、错误) 2. 其次,我创建了单独的记录器来记录每个级别的消息: public static Logger infologger = Logger.getLogger("rollingInfoFileAppender");公共静态 Logger debuglogger = Logger.getLogger("rollingAPIFileAppender"); public static Logger exceptionlogger = Logger.getLogger("rollingExceptionFileAppender");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多