How to Send Java.Util.Logging to Log4J

How to send java.util.logging to log4j?

One approach I have used successfully is to use slf4j as my primary logging API. I then have slf4j bind to log4j. 3rd party dependencies using other frameworks (like JUL) can be bridged to slf4j.

How to send java.util.logging to log4j2?

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

How do I configure log4j to send log events to java.util.logging using JULAppender?

The standard way of configuring log4j is to create log4j.xml in the root of the classpath. Here are contents of that file configured for JULAppender:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="jul" class="org.apache.log4j.JulAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n "/>
</layout>
</appender>
<root>
<priority value="all" />
<appender-ref ref="jul" />
</root>
</log4j:configuration>

Redirect log4j2 logs into java.util.logging

Why do you consider using Log4j 2 to handle the logging as fragile? Have you seen http://logging.apache.org/log4j/2.x/log4j-appserver/index.html?

No, Log4j does not provide a bridge to route logging to java.util.logging. You can route the Log4j 2 API to SLF4J and then route that to java.util.logging.

Can log4j and java util logging coexist

But I don't see any new folders being created as indicated by the Filehandler. Any one know why?

The FileHandler will not create any new folders. A directory must be created before the FileHandler will create a file.

The system property requires a path to file that is located on the filesystem It will not expand system properties or environment variables by using the dollar sign syntax.

You can use a relative path based off of the working directory or you have to use an absolute path to the logging.properties. The logging properties can not be packaged inside of an archive.

If you want to work around this limitation then you want to create a custom config class and use the java.util.logging.config.class property in conjunction with the java.util.logging.config.file property. You then write a class that reads the file://${BASE_DIR}/logging.properties and performs the needed transformation into a path to a file. Then update the configuration if you are using JDK9 or newer. On older versions you need to use readConfiguration and add code to work work around limitations of the LogManager



Related Topics



Leave a reply



Submit