Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using log4j 2.10.0 with Java 9 System.Logger

How do I setup Java 9 so that System.Logger instances write to log4j 2.10.0, instead of writing to JUL?

like image 655
XDR Avatar asked Jan 05 '18 06:01

XDR


People also ask

What is the latest version of Apache Log4j?

Log4j 2.19. 0 is the latest release of Log4j. As of Log4j 2.13. 0 Log4j 2 requires Java 8 or greater at runtime.

How do I know what version of Log4j is installed?

Navigate into the "META-INF" sub-directory and open the file "MANIFEST. MF" in a text editor. Find the line starting with "Implementation-Version", this is the Log4j version.

What is Apache Log4j 2 used for?

Starting with version 2.4, Log4j 2 provides an API for programmatic configuration The new ConfigurationBuilder API allows you to create Configurations in code by constructing component definitions without requiring you to know about the internals of actual configuration objects like Loggers and Appenders.

What is Log4j in Java used for?

Log4j is a fast, reliable and flexible logging framework which is written in java. It is an open-source logging API for java. Simply the logging means some way to indicate the state of the system at runtime.


1 Answers

This is the paragraph you refer to:

This release contains the first support of Java 9 as well as bugfixes and minor enhancements. The Log4j API was modified to use java.util.ServiceLoader to locate Log4j implementations, although the former binding mechanism is still supported. The Log4j jar is now a multi-release jar to provide implementations of the Java 9 specific classes.

It doesn't mention System.LoggerFinder and indeed a full-text search of the repo (of version 2.10) didn't turn up any mention of "LoggerFinder" and nothing promising for "java.lang.System". I'm hence sure that Log4J 2.10 does not yet integrate with JEP 264: Platform Logging API and Service.

What the release notes seem to refer to instead is that Log4J now uses the ServiceLoader API when looking for implementations of its own API.

Do it yourself

If you really need/want Log4J to log these messages, you can roll your own adapter as I did here for SLF4J. All you need are Log4J adapters for System.Logger and System.LoggerFinder (both straightforward to implement if you skimp on some of the details) and either a META-INF/services file or a module declaration like the following:

module org.slf4j.platform {
    // put the right module name here
    requires log4j;
    provides java.lang.System.LoggerFinder
        // put the right class name here
        with org.log4J.Log4JSystemLoggerFinder;
}

Launching your application with that artifact will then lead to Log4J getting platform log messages.

like image 104
Nicolai Parlog Avatar answered Oct 01 '22 15:10

Nicolai Parlog