简体   繁体   中英

Change the priority level in Log4j

Hi normally in Log4j priority levels are as follows

  • DEBUG < INFO < WARN < ERROR < FATAL

Can we change this priority levels. My requirement is I need to log only details which have priority level INFO and FATAL. Logs with priority level DEBUG, WARN and ERROR shouldn't log. If I can change the priority levels as

  • DEBUG < WARN < ERROR < INFO < FATAL

it is possible. Or is there any other way to do that. Pls help..

I was never needed to do this, but since I have once read Log4J - The Complete Manual , I hope these pointers will help.

  1. Write your own custom filter by extending org.apache.log4j.spi.Filter
  2. Override decide method
  3. Use loggingEvent.getLevel() , to identify the level of log that was triggered.
  4. Write following logic in decide

     if(event.getLevel() == Level.INFO || event.getLevel() == Level.FATAL) return ACCEPT; return DENY; 

This filter will accept all the logs that are either INFO or FATAL. You may get more creative.

Please note that I have not tested this code. This is just a guideline, hope this helps


Edit : on a bit search, I found something that might be easier. I guess, You can use LevelMatchFilter , this way

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

refer: http://wiki.apache.org/logging-log4j/Log4jXmlFormat for XML configurarion. There is a section on Filter Configuration


I don't know what stops OP in accepting this answer. I've just tested, it works:

Here is the XML.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="util" class="org.apache.log4j.FileAppender">
    <param name="File" value="D:/util.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%t %-5p %c{2} - %m%n"/>
    </layout>           

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

  </appender> 

  <root> 
    <priority value ="debug" /> 
    <appender-ref ref="util" /> 
  </root>


</log4j:configuration>

Here is the Java code

    DOMConfigurator.configure("log4j.xml");
    Logger log = Logger.getLogger(Test.class);
    log.debug("DEBUG");
    log.info("INFO");
    log.warn("WARN");
    log.error("ERROR");
    log.fatal("FATAL");

Here is the output in (as configured) D:/util.log

main INFO  test.Test - INFO
main FATAL test.Test - FATAL

Afaik, you can't change priority precedence in Log4j. But you can write your own logger implementation, which will log only needed info:

  • write your own LoggerFactory/Logger pair
  • specify LoggerFactory in log4j.properties file:

    log4j.loggerFactory=com.mycompany.mypackage.MyLog4JLoggerFactory

  • in your Logger implementation, ignore anything except INFO and FATAL errors.

However, above might be not the best solution, because it's oriented towards 1.2.x. Maybe newer versions of log4j (SLF4J) have more elegant ways to achieve it.

Found this similar question: Is it possible to log only one level messages with Log4J .

The answer hints to a LevelMatchFilter which I do not know but which might be what you need.

You can achieve this by making some changes in the log4j.properties file.
For example your default level could be FATAL and then you can specify your package or a class for INFO level.

log4j.rootLogger=FATAL, stdout, R

Statement for Package logs:
log4j.logger.com.xxx=INFO

Statement for Class logs:
log4j.logger.com.xxx.util.EmailManager=INFO

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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