Can not make Log4j2 JUL bridge work

"Maik Ebert via users" <users-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]> Tue, 29 Oct 2024 15:44:52 +0000
Newsgroups gmane.comp.lang.groovy.user
Message-ID <PA4PR07MB708846AC847AA9EACA9A1008884B2@PA4PR07MB7088.eurprd07.prod.outlook.com>
Hi,

I cannot make the log4j2 log4j-jul bridge work, so that that the logs of a library using JUL logging end up in the Log4j2 output.

My setup:
- java openjdk 11.0.24 2024-07-16 LTS
- Groovy Version: 4.0.22 JVM: 11.0.24 Vendor: Amazon.com Inc. OS: Linux

Maven pom snippet:
              <dependencyManagement>
                             <dependencies>
                                           <dependency>
                                                         <groupId>org.apache.logging.log4j</groupId>
                                                          <artifactId>log4j-bom</artifactId>
                                                          <version>2.24.1</version>
                                                          <scope>import</scope>
                                                          <type>pom</type>
                                           </dependency>
                             </dependencies>
              </dependencyManagement>
              <dependencies>
                             <dependency>
                                           <groupId>org.apache.groovy</groupId>
                                           <artifactId>groovy-all</artifactId>
                                           <version>4.0.22</version>
                                           <type>pom</type>
                             </dependency>
                             <dependency>
                                           <groupId>org.apache.logging.log4j</groupId>
                                           <artifactId>log4j-api</artifactId>
                             </dependency>
                             <dependency>
                                           <groupId>org.apache.logging.log4j</groupId>
                                           <artifactId>log4j-core</artifactId>
                             </dependency>
                             <dependency>
                                           <groupId>org.apache.logging.log4j</groupId>
                                           <artifactId>log4j-iostreams</artifactId>
                             </dependency>
                             <dependency>
                                           <groupId>org.apache.logging.log4j</groupId>
                                           <artifactId>log4j-jul</artifactId>
                             </dependency>
              </dependencies>


Linux command line for execution:

groovy -cp "../../../target/dependency/*" -Dlog4j2.configurationFile=../resources/log4j2.xml -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager myscript.groovy


Content of myscript.groovy:

import groovy.transform.Field
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

@Field
static final Logger logger = LogManager.getLogger()

logger.info System.getProperty("java.util.logging.manager")

logger.info "java.util.logging.LogManager.logManager=" + java.util.logging.LogManager.manager?.getClass()

java.util.logging.Logger.getLogger("jullogger").warning("test jul log")


Script output:

[INFO] org.apache.logging.log4j.jul.LogManager
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v9.Java9 (file:/mnt/c/pappl/sources/paysafe/ecash-platform/ecash-pipeline-groovy/src/main/groovy/../../../target/dependency/groovy-4.0.22.jar) to field java.util.logging.LogManager.manager
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v9.Java9
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] java.util.logging.LogManager.logManager=class java.util.logging.LogManager
Oct 29, 2024 4:11:59 PM org.codehaus.groovy.vmplugin.v8.IndyInterface fromCache
WARNING: test jul log


As you can see java.util.logging.LogManager.logManager is an instance of java.util.logging.LogManager, but it should be org.apache.logging.log4j.jul.LogManager as configured on the Groovy command line via the system property.
You can also see that the “test jul log” output does use the standard JUL format, as it is not forwarded to the Log4J2 system.

It looks like the code which initializes static final java.util.logging.LogManager.manager field in a static code block, retrieves System.getProperty("java.util.logging.manager") as null, and therefore fallbacks to the default JUL LogManager.
Is this related to the dynamic compilation approach I use here?

I also tried to set the java.util.logging.manager system property through a compiler configscript on the command line, but it had no positive effect.

I also tried to use -Dlog4j2.debug=true to gather more helpful information, but there was nothing helpful.
As mentioned above, it must be somehow related to the static initialization and the system property.

The basically same setup works fine when run through a Groovy run config in IntelliJ.

Thanks a lot,
Maik