Using log4j 2.10.0 with Java 9 System.Logger

XDR :

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

Nicolai :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=465163&siteId=1