views:

106

answers:

4

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.

+1  A: 

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  
CoolBeans
It is the other way around. The root logger usually enables everything, and you then need to turn off those you do not want. Hence the =ERROR so all at lower levels do not show.
Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen: Please read what I said. I told OP to take them out if he has any of these entries since he didnt want to see any messages from those packages.
CoolBeans
Putting lines similar to those in, will prevent the logging of debug or info message for those packages, even if debug/info was defined for the rootLogger.
nhnb
@CoolBeans, I read what you said. It is the other way around.
Thorbjørn Ravn Andersen
Okay so are you saying if I have log4j.rootLogger=FATAL and I do NOT have any spring packages listed in my log4j.properties, it will still display messages from spring? Just making sure I make myself clear.
CoolBeans
A: 

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

nhnb
+1  A: 

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>
YoK
so level off can be set for parent package like in this case if I don't want logs from all packages starting with org.springframework I will set its level as off.
YoK
+2  A: 

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.

Nathan Hughes