Java 过滤掉来自第三方框架的 log4j 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filtering out log4j messages from third-party frameworks?
提问by walters
How do I filter log messages from external third party frameworks? I am using Hibernate and Spring framework and I would like to suppress the logs so that only my log4j logs appears.
如何过滤来自外部第三方框架的日志消息?我正在使用 Hibernate 和 Spring 框架,我想抑制日志,以便只显示我的 log4j 日志。
采纳答案by Nathan Hughes
In my log4j.properties file I set the root logger logging level to ERROR. Then for packages I specifically want to log, like my application code, I set the logging level to INFO or DEBUG.
在我的 log4j.properties 文件中,我将根记录器日志记录级别设置为 ERROR。然后对于我特别想要记录的包,比如我的应用程序代码,我将日志记录级别设置为 INFO 或 DEBUG。
log4j.rootLogger=ERROR, stdout
log4j.logger.com.initech.tps=DEBUG
log4j.logger.org.hibernate.SQL=INFO
I see co-workers who set root logging low and then end up listing everything they don't want to see, that just seems backward to me. I would rather list what I want to log than all the things I don't want to log.
我看到同事将 root 日志记录设置得很低,然后最终列出了他们不想看到的所有内容,这对我来说似乎是倒退。我宁愿列出我想记录的内容而不是我不想记录的所有内容。
BTW turning logging off entirely for a third-party component seems like a bad idea to me. For instance, Spring is relatively noisy and uses WARN for things I really don't need to know about, but if it logs an ERROR entry for something I want to see it.
顺便说一句,完全关闭第三方组件的注销对我来说似乎是一个坏主意。例如,Spring 相对嘈杂,并且将 WARN 用于我真正不需要知道的事情,但如果它记录了我想查看的内容的 ERROR 条目。
回答by CoolBeans
Just don't add those packages in your log4j.properties. For instance, you must have this for Spring in your properties file. Take it out if you have some entries like below (anything that starts with org.springframework). Same needs to be done for hibernate.
只是不要在 log4j.properties 中添加这些包。例如,你必须在你的属性文件中为 Spring 设置这个。如果您有以下条目(任何以 org.springframework 开头的条目),请将其取出。休眠也需要做同样的事情。
#Logs the SQL from Spring
log4j.logger.org.springframework.jdbc.core.JdbcTemplate=ERROR
#Logs the SQL parameters from Spring
log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=ERROR
Also as it was mentioned you should also set the following. I forgot to mention this.
此外,正如提到的,您还应该设置以下内容。我忘了提到这一点。
log4j.rootLogger=FATAL
or
log4j.rootLogger=ERROR
回答by Hendrik Brummermann
In log4j.properties you can define individual levels on a per logger basis:
在 log4j.properties 中,您可以在每个记录器的基础上定义各个级别:
log4j.logger.<name>=FATAL
In log4j.xml the syntax is
在 log4j.xml 中,语法是
<logger name="<name>">
<level value="fatal"/>
</logger>
<name> is often the full qualified classname. You might want to use WARN or ERROR instead of FATAL
<name> 通常是完全限定的类名。您可能想使用 WARN 或 ERROR 而不是 FATAL
回答by YoK
You can do it by changing logger level in log4j.properties/log4j.xml file.
您可以通过更改 log4j.properties/log4j.xml 文件中的记录器级别来实现。
You need to set logger's <level value="off"/>
if you want to filter logs from package but keep logger configuration for later use.
You could also set it to highest level to log only in case of error or fatal issue.
<level value="off"/>
如果您想从包中过滤日志但保留记录器配置以供以后使用,则需要设置记录器。您还可以将其设置为最高级别,以便仅在出现错误或致命问题时进行记录。
Following entries should be added to log4j.xml to turn off logging from hibernate and springframework packages:
以下条目应添加到 log4j.xml 以关闭来自 hibernate 和 springframework 包的日志记录:
<logger name="org.springframework">
<level value="off"/>
</logger>
<logger name="org.hibernate">
<level value="off"/>
</logger>