71

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Damian
  • 2,930
  • 6
  • 39
  • 61
  • 1
    If you've got the time I'd recommend looking at SLF4J. http://www.slf4j.org/manual.html - it acts as a meta-layer above an actual logging framework - including log4j. Its very easy to set up. Yes - it is possible with log4j to set logging levels at package level. It also supports an 'off' level. – David Victor Feb 11 '11 at 19:16
  • 1
    Thanks, I wish I could have approve this answer, but this is a comment.. – Damian Feb 11 '11 at 21:35

4 Answers4

121

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...
Arek
  • 3,106
  • 3
  • 23
  • 32
20

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.

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
Daniel
  • 27,718
  • 20
  • 89
  • 133
  • I'd like to do it in simplelog. But I see that changing it to log4j is a solution. – Damian Feb 11 '11 at 21:30
  • 1
    log4j is the most used logging framework in the world. Just stick with what everyone uses, and you profit from plugins, custom appenders, filters, etc. – Daniel Feb 11 '11 at 21:39
  • 2
    The key term is `OFF` which is missing from top-voted answer. – kevinarpe Dec 20 '17 at 00:43
17

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

Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging.log-levels

Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87
1

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

Documentation here and here.

jt.
  • 7,625
  • 4
  • 27
  • 24
  • I know. The problem is how to do sth like "log4j.logger. = DEBUG|INFO|OFF|WARN..." in SimpleLog. – Damian Feb 11 '11 at 21:27
  • I created a small project and tested package-level log control and it works as desired. I have updated my example to illustrate hierarchical log level control. If it is not working this way in your environment, it is likely not detecting your simplelog.properties. When you change other values in there, does it affect the output (e.g. enable/disable dates w/ org.apache.commons.logging.simplelog.showdatetime=true/false)? – jt. Feb 12 '11 at 20:53