log4j migration to log4j2

2019-07-11 13:04发布

问题:

Log4j migration guide

https://logging.apache.org/log4j/2.x/manual/migration.html

states that

Calls to org.apache.log4j.Logger.getLogger that accept a LoggerFactory must remove the org.apache.log4j.spi.LoggerFactory and use one of Log4j 2's other extension mechanisms.

What are the extension mechanisms available in log4j2 and how best to migrate a method like below in log4j2

private static TraceLoggerFactory stTraceFactory = new TraceLoggerFactory();

public static Logger getTraceLogger(final String name) {
    return getLogger(name, stTraceFactory);
}


class TraceLoggerFactory implements LoggerFactory {
    @Override
    public Logger makeNewLoggerInstance(final String name) {
        return new TraceLogger(name);
    }
}

回答1:

The code you posted doesn't show the use of the getLogger method that accepts a LoggerFactory so I'll just assume you are calling the getLogger method and passing it your TraceLoggerFactory.

The "other extension mechanisms" are listed on the Extending Log4j page.

As far as how best to migrate your code, it's not really possible to give recommendations without understanding the purpose of your current customization. For example - what is the purpose of TraceLogger?

I suggest you take the time to research log4j in more detail and determine if you really need your customization or if you can use the provided features of log4j instead. Customization increases your long term maintenance and should only be done when absolutely necessary.