1

I am using log4j2 to do some logging for a project. My Loggers section in my config looks like this.

<Loggers>

    <!-- default logger -->
    <Root level="info">
        <AppenderRef ref="Console"/>
    </Root>

    <!-- only log warn/error/fatal for external libs -->
    <Logger name="org.cfg4j" level="warn" additivity="false">
        <AppenderRef ref="Console"></AppenderRef>
    </Logger>
    <Logger name="io.netty.util" level="warn" additivity="false">
        <AppenderRef ref="Console"></AppenderRef>
    </Logger>

</Loggers>

This doesn't seem to be having any effect as far as supressing the DEBUG level messages from io.netty.util

I googled "io.netty.util logging" and came up with this. It looks like there is a way to override the default logger

public abstract class InternalLoggerFactory {
private static volatile InternalLoggerFactory defaultFactory;

static {
    final String name = InternalLoggerFactory.class.getName();
    InternalLoggerFactory f;
    try {
        f = new Slf4JLoggerFactory(true);
        f.newInstance(name).debug("Using SLF4J as the default logging framework");
        defaultFactory = f;
    } catch (Throwable t1) {
        try {
            f = new Log4JLoggerFactory();
            f.newInstance(name).debug("Using Log4J as the default logging framework");
        } catch (Throwable t2) {
            f = new JdkLoggerFactory();
            f.newInstance(name).debug("Using java.util.logging as the default logging framework");
        }
    }

    defaultFactory = f;
}

I tried giving this a go, butI ran into a few issues. For one, my IDE (intellij) told me that Log4JLoggerFactory is deprecated. I proceeded, but then found out that the instance did not contain a method called "newInstance".

So, I'm a little confused on how to proceed with this. Is there a way for log4j to supress external libraries DEBUG/INFO level messages in the way that I am thinking?

Zack
  • 13,454
  • 24
  • 75
  • 113

1 Answers1

1

If the logger is defined in log4j 1.x api ( original version of the answer):

The problem is that log4j 1.x api don't use log4j2 property.

Add log4j-1.2-api.jar, as a log4j 1.x bridge, it should fix your problem.

But seems default loggerfactory of netty is Slf4JLoggerFactory, so waht actually needed are the slf4j bridges. slf4j-log4j12 to log4j 1.x, log4j-slf4j-impl to log4j2.x.

Mobility
  • 3,117
  • 18
  • 31
  • compile "org.apache.logging.log4j:log4j-1.2-api:+" I put this in my gradle build file. Does this look correct? – Zack Jun 16 '17 at 15:10
  • Looks like the external lib (the bridge) is indeed in my class path, but I'm still getting a bunch of logging output related to the external libs – Zack Jun 16 '17 at 19:48
  • According to [this](https://logging.apache.org/log4j/log4j-2.2/log4j-1.2-api/index.html), I need to replace all instances of the 1.0 distribution with the bridge. Would I have to dive into each external lib and do this?? – Zack Jun 16 '17 at 19:48
  • @Zack seems default loggerfactory of netty is Slf4JLoggerFactory, so if there is a chance some other jars in your classpath can implement the slf4j api, like logback, for example. If there is any,remove them. – Mobility Jun 19 '17 at 03:16
  • This appeared to get me in the right direction. In addition to the log4j-1.2-api.jar, I needed to also include slf4j-log4j12 jar. Not sure at all what this does, but my log messages are now appearing like I want them to. – Zack Jun 19 '17 at 17:26
  • @Zack. slf4j-log4j12 jar is a bridge from slf4j api to log4j1.x. Just a log4j-slf4j-impl jar should also work ,it's a bridge from slf4j to log4j2 directly. I should complete the answer. – Mobility Jun 20 '17 at 01:15
  • Thanks for the explanation. – Zack Jun 20 '17 at 15:14
  • [similar issue](https://stackoverflow.com/questions/44677501/how-do-i-disable-jose4j-log-messages-through-my-log4j-config) if you could please have a look :) – Zack Jun 21 '17 at 13:28