[英]Change the priority level in Log4j
Hi normally in Log4j priority levels are as follows 嗨,通常在Log4j中,优先级如下
Can we change this priority levels. 我们可以改变这个优先级。 My requirement is I need to log only details which have priority level INFO and FATAL. 我的要求是我只需要记录优先级为INFO和FATAL的详细信息。 Logs with priority level DEBUG, WARN and ERROR shouldn't log. 优先级为DEBUG,WARN和ERROR的日志不应记录。 If I can change the priority levels as 如果我可以将优先级更改为
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. 我从来不需要这样做,但因为我曾经读过Log4J - 完整手册 ,我希望这些指针能有所帮助。
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. 此过滤器将接受INFO或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 我猜,你可以用这种方式使用LevelMatchFilter
<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. 参考: http : //wiki.apache.org/logging-log4j/Log4jXmlFormat for XML configurarion。 There is a section on Filter Configuration
有一个关于Filter Configuration
的部分
I don't know what stops OP in accepting this answer. 我不知道什么阻止OP接受这个答案。 I've just tested, it works: 我刚刚测试过,它有效:
Here is the XML. 这是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 这是Java代码
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
这是(如配置) D:/util.log
的输出
main INFO test.Test - INFO
main FATAL test.Test - FATAL
Afaik, you can't change priority precedence in Log4j. Afaik,你不能改变Log4j中的优先级优先级。 But you can write your own logger implementation, which will log only needed info: 但是你可以编写自己的记录器实现,它只记录所需的信息:
LoggerFactory/Logger
pair 编写自己的LoggerFactory/Logger
对 specify LoggerFactory in log4j.properties
file: 在log4j.properties
文件中指定LoggerFactory:
log4j.loggerFactory=com.mycompany.mypackage.MyLog4JLoggerFactory log4j.loggerFactory = com.mycompany.mypackage.MyLog4JLoggerFactory
However, above might be not the best solution, because it's oriented towards 1.2.x. 但是,上面可能不是最好的解决方案,因为它面向1.2.x. Maybe newer versions of log4j (SLF4J) have more elegant ways to achieve it. 也许更新版本的log4j(SLF4J)有更优雅的方式来实现它。
Found this similar question: Is it possible to log only one level messages with Log4J . 发现了类似的问题: 是否可以使用Log4J仅记录一个级别的消息 。
The answer hints to a LevelMatchFilter which I do not know but which might be what you need. 答案暗示了LevelMatchFilter ,我不知道,但可能是你需要的。
You can achieve this by making some changes in the log4j.properties file. 您可以通过在log4j.properties文件中进行一些更改来实现此目的。
For example your default level could be FATAL and then you can specify your package or a class for INFO level. 例如,您的默认级别可能是FATAL,然后您可以指定您的包或INFO级别的类。
log4j.rootLogger=FATAL, stdout, R log4j.rootLogger = FATAL,stdout,R
Statement for Package logs: 包日志声明:
log4j.logger.com.xxx=INFO log4j.logger.com.xxx = INFO
Statement for Class logs: 类日志声明:
log4j.logger.com.xxx.util.EmailManager=INFO log4j.logger.com.xxx.util.EmailManager = INFO
Hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.