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
!
The JDK Logging Adapter is a custom implementation of java. util. logging. LogManager that uses Log4j.
Logging is used to store exceptions, information, and warnings as messages that occur during the execution of a program. Logging helps a programmer in the debugging process of a program. Java provides logging facility in the java. util. logging package.
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:
put the following jars in the classpath: log4j-api-2.x, log4j-core-2.x and log4j-jul-2.x.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With