简体   繁体   中英

How to send java.util.logging to log4j2?

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 !

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM