I'm working on a project that uses the NetBeans Platform. Under the hood, NetBeans uses the java.util.logging framework.

I would like to intercept these log calls by binding java.util.logging to log4j2. Is this possible?

Previous questions have been asked that are similar, but were specific to log4j and not log4j2!

有帮助吗?

解决方案 2

I've found a solution:

The NetBeans Platform names logger after the org.netbeans.* namespace. To route NetBeans log calls, I simply created a Handler and registered it with the org.netbeans logger:

public class CustomHandler extends Handler
{
    @Override
    public void publish(LogRecord record)
    {
        // Re-direct log calls here (e.g. send record to Log4j2 logger).
    }

    @Override
    public void flush() 
    {
    }

    @Override
    public void close() throws SecurityException
    {
    }
}

Be sure to register the new Handler and disable parent loggers if necessary:

Logger logger = Logger.getLogger("org.netbeans");
logger.addHandler(new CustomerHandler());
logger.setUseParentHandlers(false);

其他提示

UPDATE 2014/9/12: From version 2.1, Log4j 2 contain a JUL bridge that does not require SLF4J.

No custom code is required. Two things:

  1. put the following jars in the classpath: log4j-api-2.x, log4j-core-2.x and log4j-jul-2.x.

  2. set the system property java.util.logging.manager to org.apache.logging.log4j.jul.LogManager before any calls are made to LogManager or Logger

The Log4J2 FAQ page shows the dependencies visually: http://logging.apache.org/log4j/2.x/faq.html#which_jars

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top