简体   繁体   English

Log4J特定文件的特定级别 - appender

[英]Log4J specific levels to specific files - appenders

I am using log4j for one of my projects and I know it is possible to set thresholds for each appender. 我正在为我的一个项目使用log4j,我知道可以为每个appender设置阈值。

My objective is to log only INFO messages into a file and only ERROR messages in another file. 我的目标是仅将INFO消息记录到文件中,而仅将ERROR消息记录到另一个文件中。

The problem of using threshold's for this is that if i set a appender threshold to INFO and the other to ERROR, when I log an ERROR message, that message goes to both files (appenders). 使用阈值的问题是,如果我将一个appender阈值设置为INFO而另一个设置为ERROR,那么当我记录一条ERROR消息时,该消息会转到两个文件(appenders)。

How can I achieve this? 我怎样才能做到这一点?

Thanks in advance 提前致谢

We use differents logger for different levels using: 我们使用不同级别的不同记录器:

log4j.appender.XXXTracking.filter.LevelToMatch=INFO log4j.appender.XXXTracking.filter.LevelToMatch = INFO

with the option filter.LevelToMatch you could write the level that you want. 使用选项filter.LevelToMatch,您可以编写所需的级别。 This in the log4j.properties 这在log4j.properties中

You'll want to use org.apache.log4j.varia.LevelMatchFilter 您将要使用org.apache.log4j.varia.LevelMatchFilter

<filter class="org.apache.log4j.varia.LevelMatchFilter">
            <param name="LevelToMatch" value="ERROR" />
            <param name="AcceptOnMatch" value="true" />
        </filter>

Thanks for the response guys, 谢谢你的回应,

What i was doing wrong was that I was trying to define the filter on a properties file. 我错误的是我试图在属性文件上定义过滤器。 Since the use of this is only suported on an XML configuration file, I have changed to an XML file. 由于使用它仅支持XML配置文件,因此我已更改为XML文件。

To have exactly what I needed I also had to add a DenyAllFilter filter type: 为了得到我需要的东西,我还必须添加一个DenyAllFilter过滤器类型:

        <filter class="org.apache.log4j.varia.LevelMatchFilter"> 
            <param name="LevelToMatch" value="INFO" /> 
            <param name="AcceptOnMatch" value="true" />                        
        </filter>
        <filter class="org.apache.log4j.varia.DenyAllFilter" />

Thanks for the help guys 谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM