Log4j 1.2多层过滤 [英] Log4j 1.2 multi-level filtering

查看:36
本文介绍了Log4j 1.2多层过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

顺便说一句,这是log4j 1.2.

BTW, this is log4j 1.2.

我的应用程序每次创建警报时都需要记录日志,而我们希望在INFO级别进行记录.我们的根记录器的优先级设置为INFO.到现在为止还挺好.我们添加了一些满足特殊需求的附加程序,包括标准CONSOLE附加程序和server.log附加程序(及其他).我已经使用控制台和server.log附加程序配置了根记录程序.到目前为止也可以.

My application needs to log every time we create an alert, and we want to do it at INFO level. Our root logger has priority level set to INFO. So far so good. We have added several appenders for specialized needs, including a stadard CONSOLE appender and a server.log appender (and others). I have configured the root logger with the console and server.log appenders. Also fine up to this point.

   <root>
      <priority value="INFO"/>
      <appender-ref ref="CONSOLE"/>
      <appender-ref ref="server"/>
      <appender-ref ref="logfileForErrors"/>
   </root>

现在,我需要一个新的附加程序,该附加程序通常可以将所有ERROR/WARNING/FATAL都发送到server.log,还需要特定类的INFO.我从具有阈值INFO的附加程序开始,所以我可以接受警报INFO日志:

Now I need a new appender that gets all of ERROR/WARNING/FATAL that normally goes to server.log, but also INFOs for a specific class. I started with an appender with threshold of INFO so I could accept the alarm INFO logs:

<appender name="logfileForAlarmsAndOther" class="org.apache.log4j.RollingFileAppender">
    <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
    <param name="file" value="${biomerieux.data}/simon/logs/vilink.log" />
    <param name="append" value="true" />
    <param name="threshold" value="INFO" />
    <param name="maxBackupIndex" value="1" />
</appender>

然后我为我的课堂定义了一个记录器:

Then I defined a logger for my class:

    <logger name="my.root.path.service.alert.AlertService" additivity="false">
        <level value="info" />
        <appender-ref ref="logfileForAlarmsAndOther"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="server"/>
    </logger>

因此,我成功地在所有列出的日志中获取了警报日志(由于根目录已经位于INFO上,所以很多工作都没有做).但是,我真正想要的是,如果它们来自一个特定的类,则现在只能在新的附加器 上获得INFO.

So I successfully get my alert logs in all of the listed logs (a lot of work for nothing since the root was already at INFO). But what I really want is to now get INFOs at my new appender only if they are from the one specific class.

我不确定我对此的描述是否很好,并且我不太懂log4j,因此如果需要,请进行说明.难题与将我的新追加程序限制为WARNING或更高级别有关,除了我希望来自特定类的INFO.并且此类的这些INFO还应该传递给父记录器通常会记录INFO的每个追加程序.我可能会感到困惑,但是似乎可以在log4j 1.2中使用自定义过滤器.

I am not sure I have described this well, and I am not log4j savvy, so please ask for clarifications if needed. The conundrum has to do with limiting my new appender to WARNING and greater, except I also want INFOs from a specific class. And these INFOs for this class should also go to every appender that parent loggeres would normally log INFOs. I may be confused, but it seems like a custom filter is all I can think of to work in log4j 1.2.

推荐答案

我解决此问题的方法是消除特定类的多余子记录器,而在附加器中使用一系列过滤器来处理:

The way I solved this was to eliminate the extra child logger for the specific class and handle instead with a series of filters within the appender:

<appender name="logfileForAlarmsAndOther" class="org.apache.log4j.RollingFileAppender">
    <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
    <param name="file" value="${biomerieux.data}/simon/logs/vilink.log" />
    <param name="append" value="true" />
    <param name="threshold" value="INFO" />
    <param name="maxBackupIndex" value="1" />
    <filter class="org.apache.log4j.varia.StringMatchFilter">
        <param name="StringToMatch" value="MyStringToMatch" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="WARN" />
    </filter>
</appender>

因此,全局阈值是INFO,它立即接受与String匹配的INFO,但是其他任何内容都将受到第二个过滤器的限制,该过滤器将剩余消息限制为WARN或更高.

So the global threshold is INFO, and it immediately accepts INFOs that match the String, but anything else is subject to the second filter which limits remaining messages to WARN or above.

这样做的缺点是我必须在日志消息中引入一个唯一的可搜索字符串.

The downside of this is that I had to introduce a unique searchable String in the log message.

这篇关于Log4j 1.2多层过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆