How to disable loggers of a class or of whole package?
Asked Answered
C

5

76

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

Chemosphere answered 11/2, 2011 at 19:3 Comment(2)
If you've got the time I'd recommend looking at SLF4J. 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.Cauvery
Thanks, I wish I could have approve this answer, but this is a comment..Chemosphere
W
125

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...
Wanyen answered 11/2, 2011 at 19:16 Comment(1)
How can I get a handle to this logger in my class?Deface
S
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.

Storfer answered 11/2, 2011 at 19:28 Comment(3)
I'd like to do it in simplelog. But I see that changing it to log4j is a solution.Chemosphere
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.Storfer
The key term is OFF which is missing from top-voted answer.Deice
S
18

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

Schramm answered 20/1, 2017 at 8:49 Comment(0)
D
4

I am using Log4j2 and the configuration file I use with is resources/log4j2.xml.

My problem was disabling all logs from a third-party dependency com.pesky.and.chatty.package which was outputting lots of logs in DEBUG and INFO levels.

I searched far and wide until I found how to disable all logs from that package.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%p %m%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="org.apache.http" level="warn" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="com.pesky.and.chatty.package" level="OFF" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

Not a single peep from com.pesky.and.chatty.package anymore!

Note additivity set to OFF for the given Logger. This ensures the logs aren't propagated to the Root and won't end up printed there. Really sneaky issue to debug!

Demodena answered 21/12, 2023 at 0:6 Comment(2)
log4j2.properties didn't work for me in my maven project but this solution did. Thanks.Erny
@Erny It depends what logging framework you use and which one is configured for the project. In my case, my pom.xml also had to include some extra packages to make sure log4j2 is properly set as the logger.Demodena
S
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.

Salted answered 11/2, 2011 at 20:29 Comment(2)
I know. The problem is how to do sth like "log4j.logger.<your package> = DEBUG|INFO|OFF|WARN..." in SimpleLog.Chemosphere
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)?Salted

© 2022 - 2024 — McMap. All rights reserved.