How to disable loggers of a class or of whole package?
JavaApache Commons-LoggingJava Problem Overview
I am using Apache Commons Logging ™. For now I wanted to use SimpleLog
implementation, but when I changed the level, loggers from the libraries came out. I want it to turn them off.
Is there a easy way to change log level for whole package (can Log4j do that)?
I have tried to set
> org.apache.commons.logging.simplelog.log.foo=fatal
in the property files to disable (setting to fatal is OK) foo
logger, but it doesn't work (foo is a name of logger that appears in output : [INFO] foo - Message
).
Java Solutions
Solution 1 - Java
In Log4j you can specify a logging level for specified package, class or logger identified by string. You just simply write this in log4j.properties file:
log4j.logger.<your package> = DEBUG|INFO|OFF|WARN...
Solution 2 - Java
You should use:
log4j.logger.foo = OFF
Please note that "foo" does not need to be a package, or a class, but is an arbitrary String. We e.g. have a logger named "SQL" that is called from many classes.
Solution 3 - Java
If you use Spring Boot, you may set to OFF
in application.properties file, by using logging.level.<package-or-class-name>=OFF
Example:
logging.level.org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer=OFF
Solution 4 - Java
Use of SimpleLog from Commons Logging requires two configuration files unless you are using some system properties. The files are: commons-logging.properties and simplelog.properties. The log level properties you have indicated should be placed in simplelog.properties like:
org.apache.commons.logging.simplelog.log.foo=warn
where "foo" is the logger name. Generally, this is the package or package and class name. In the following example, everything under the com.stackoverflow.utils package is set to info whereas com.stackoverflow.servlet.Dispatcher is specifically set to warn:
org.apache.commons.logging.simplelog.log.com.stackoverflow.utils=info
org.apache.commons.logging.simplelog.log.com.stackoverflow.servlet.Dispatcher=warn
The commons-logging.properties file should contain:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog