开发者

Filtering out log4j messages from third-party frameworks?

How do I filter log messages from external third party frameworks? I 开发者_StackOverflowam using Hibernate and Spring framework and I would like to suppress the logs so that only my log4j logs appears.


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.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.

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.


You can do it by changing logger level in log4j.properties/log4j.xml file.

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.

Following entries should be added to log4j.xml to turn off logging from hibernate and springframework packages:

<logger name="org.springframework">
    <level value="off"/>
</logger>
<logger name="org.hibernate">
    <level value="off"/>
</logger>


In log4j.properties you can define individual levels on a per logger basis:

log4j.logger.<name>=FATAL

In log4j.xml the syntax is

<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


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.

#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  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜