Reputation: 2848
Hi normally in Log4j priority levels are as follows
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
it is possible. Or is there any other way to do that. Pls help..
Upvotes: 11
Views: 27421
Reputation: 55856
I was never needed to do this, but since I have once read Log4J - The Complete Manual, I hope these pointers will help.
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
Upvotes: 26
Reputation: 2183
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.
Upvotes: 0
Reputation: 49341
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.
Upvotes: 0
Reputation: 11996
Afaik, you can't change priority precedence in Log4j. But you can write your own logger implementation, which will log only needed info:
LoggerFactory/Logger
pairspecify LoggerFactory in log4j.properties
file:
log4j.loggerFactory=com.mycompany.mypackage.MyLog4JLoggerFactory
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.
Upvotes: 0