geri-m
geri-m

Reputation: 695

Netty 4.0 and Log4J

I'm using Netty 4.0 (CR9) running in a Tomcat 6.0 application. I'm using Log4J thru Slf4J. All Componentes in my application use Log4J, desipte Netty. I get the following message during startup:

log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I do have the following lines of code in the startup listener

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();"

as I do need them for Jersey to corretly use Slf4J.

I already had a look at the following questions, which did not solve my problem

Upvotes: 4

Views: 12458

Answers (2)

robert
robert

Reputation: 4867

For anybody that is configuring their logging programmatically (as I am, for the sake of some unit-tests) it may not be desirable to have to go and set up a log4j.xml

In the case of Netty a number of the classes try to access their Loggers at class loading stage so you will have to ensure that whatever programmatic initialisation you want to do happens beforehand.

So for instance, if you are declaring a Netty static member, ensure your static initialisation happens beforehand (ordering of members in the class is relevant) e.g.

static {

    Logging.initialise();
}

static ByteBufAllocator alloc = new UnpooledByteBufAllocator(true);

If its a common utility class that you don't want to touch and implement the programmatic logging later in initialisation you could perhaps set a null appender:

static {

    /* Eliminate Log4j warning when instantiating 'alloc' below */
    Logger rootLogger = Logger.getRootLogger();

    /* Check that an appender hasn't already been set */
    if (!rootLogger.getAllAppenders().hasMoreElements()) {

        rootLogger.addAppender(new NullAppender());
    }
}

static ByteBufAllocator alloc = new UnpooledByteBufAllocator(true);

Note that if an Appender is already set, don't do anything because in that case it's safe to assume somebody has configured it elsewhere.

Upvotes: 2

geri-m
geri-m

Reputation: 695

I found the solution to my problem in this question: Where to place log4j.xml. If you use "-Dlog4j.debug" as a parameter for building the web-app and it shows you which appenders are used.

The Problem in my case was the the log4j.xml was correctly found, but an incorrect version. (missing the correct appender). By adding the appender to version used by log4j it works.

My Appender looks something like

<category name="io.netty">
    <priority value="warn"/>
    <appender-ref ref="Console"/>
</category>

Upvotes: 1

Related Questions