Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log4j2.disableJmx and/or log4j2.disable.jmx seem not to work #2774

Closed
autumoswitzerland opened this issue Jul 27, 2024 · 8 comments · Fixed by #2775
Closed

log4j2.disableJmx and/or log4j2.disable.jmx seem not to work #2774

autumoswitzerland opened this issue Jul 27, 2024 · 8 comments · Fixed by #2775
Assignees
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Milestone

Comments

@autumoswitzerland
Copy link

Description

System property switches "log4j2.disableJmx" and/or "log4j2.disable.jmx" seem not to work!

Configuration

Version: 2.23.1 with slf4j 1.7.36

Operating system: Linux Mint 21.2 Cinnamon

JDK: Azu Zulu Build of OpenJDK - Zulu17.52+17-CA 17.0.12+7-LTS, Java version 17.0.12

Logs

2024-07-27T21:19:27.967946158Z main ERROR Could not reconfigure JMX java.lang.NoClassDefFoundError: javax/management/InstanceNotFoundException
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:642)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:260)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:154)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:46)
	at org.apache.logging.log4j.LogManager.getContext(LogManager.java:197)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:136)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:58)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:32)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
	at ch.autumo.ifacex.studio.IfaceXStudio.<clinit>(IfaceXStudio.java:77)
	at ch.autumo.ifacex.studio.Main.main(Main.java:26)
Caused by: java.lang.ClassNotFoundException: javax.management.InstanceNotFoundException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
	at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
	... 14 more

2024-07-27T21:19:28.390794174Z main ERROR Could not reconfigure JMX java.lang.NoClassDefFoundError: javax/management/InstanceNotFoundException
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:642)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:739)
	at org.apache.logging.log4j.core.config.Configurator.reconfigure(Configurator.java:263)
	at ch.autumo.commons.logging.Log4j2Logging.initializeInMemory(Log4j2Logging.java:184)
	at ch.autumo.commons.logging.Log4j2Logging.initializeInMemoryFile(Log4j2Logging.java:87)
	at ch.autumo.ifacex.studio.IfaceXStudio.main(IfaceXStudio.java:797)
	at ch.autumo.ifacex.studio.Main.main(Main.java:26)
Caused by: java.lang.ClassNotFoundException: javax.management.InstanceNotFoundException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
	at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
	... 7 more

Java-Code:

    public static void main(String[] args) {
        System.setProperty("log4j2.disable.jmx", "true");
        System.setProperty("log4j2.disableJmx", "true");
        ...

Later:

org.apache.logging.log4j.core.config.Configurator.reconfigure(builder.build());

Reproduction

It's a linux jpackaged executable. Those JMX bean class-not-found-exceptions are only triggered on linux, the same code on Windows and mac doesn't even seem to miss any JMX code. The log4j2-JMX disable switches seem to have no effect. I wonder if they are even filled into the log4j component properties ?!

@vy vy self-assigned this Jul 28, 2024
@vy
Copy link
Member

vy commented Jul 28, 2024

@autumoswitzerland, thanks for your report, much appreciated. The described behaviour is indeed unexpected. I will create a custom Java runtime and try to reproduce the error – when JMX is disabled, the java.management module (providing InstanceNotFoundException) should not be required.

In the meantime,

  1. Could you share the packaged modules (java.base, java.compiler, etc.) in your Windows and Linux Java runtimes?
  2. Could you try setting the LOG4J_DISABLE_JMX=true environment variable to cross out the initialization order hiccup?
  3. Could you try (explicitly?) adding the java.management module?

@vy vy added waiting-for-user More information is needed from the user and removed waiting-for-maintainer labels Jul 28, 2024
@vy vy added the bug Incorrect, unexpected, or unintended behavior of existing code label Jul 28, 2024
@vy vy added this to the 2.24.0 milestone Jul 28, 2024
@ppkarwasz
Copy link
Contributor

The code seems to work as expected, the only problem I see is the severity of the status logger call, which should not be ERROR but INFO or DEBUG:

try {
Server.reregisterMBeansAfterReconfigure();
} catch (final LinkageError | Exception e) {
// LOG4J2-716: Android has no java.lang.management
LOGGER.error("Could not reconfigure JMX", e);
}

public static void reregisterMBeansAfterReconfigure() {
// avoid creating Platform MBean Server if JMX disabled
if (isJmxDisabled()) {
LOGGER.debug("JMX disabled for Log4j2. Not registering MBeans.");
return;
}
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
reregisterMBeansAfterReconfigure(mbs);
}

Unless I am mistaken the Server.reregisterMBeansAfterReconfigure() can throw a linkage error at any moment:

  • a very eager JVM could throw the exception when the Server class is initialized (if it resolves all the references in its methods)
  • a less eager JVM could throw the exception when the reregisterMBeansAfterReconfigure() method is called,
  • only a very lazy JVM will evaluate the if statement on line 135.

To solve this issue I would propose to test the value of log4j2.disableJmx configuration property in the LoggerContext class, before we even reference the Server class.

@vy
Copy link
Member

vy commented Jul 28, 2024

@autumoswitzerland, I have verified that log4j-core still requires the java.management module even when JMX is disabled. I have fixed this issue in #2775, which will be released with the next Log4j version, that is, 2.24.0. Note that JMX is disabled by default in 2.24.0 😉, hence you won't need to explicitly disable it anymore.

As a temporary workaround, you need to add java.management module to your runtime.

@autumoswitzerland autumoswitzerland changed the title log4j2.disableJmx and/or log4j2.disable.jmx seems not to work log4j2.disableJmx and/or log4j2.disable.jmx seem not to work Jul 28, 2024
@autumoswitzerland
Copy link
Author

Thanks for the quick response and checking of the issue! It is not really critical, it was more a surprising behaviour on Linux.
Do you still need any of the artefacts (1-3) you requested above?

Still wondering why the switch is not in the log4j2 component properties, because that is imho the real issue; The system property is not copied into the component properties. Therefore, isJmxDisabled = false, at least on Linux! If it were true as expected, the dependency to java.management would be irrelevant. No?

@github-actions github-actions bot added waiting-for-maintainer and removed waiting-for-user More information is needed from the user labels Jul 28, 2024
@autumoswitzerland
Copy link
Author

Addendum, request No. 2. See attached image.
Bildschirmfoto 2024-07-29 um 00 07 06

@vy vy closed this as completed in #2775 Jul 29, 2024
@vy
Copy link
Member

vy commented Jul 29, 2024

Still wondering why the switch is not in the log4j2 component properties, because that is imho the real issue; The system property is not copied into the component properties.

No, that is not the issue. The temporary workaround (until 2.24.0 is released) for you is to include java.management module to your custom Java runtime. @autumoswitzerland, would you mind trying that out and reporting us the outcome, please?

@ppkarwasz
Copy link
Contributor

As a temporary workaround, you need to add java.management module to your runtime.

IMHO as a temporary workaround the status logger ERROR messages can be ignored and treated as false positive WARN messages.

@autumoswitzerland
Copy link
Author

Yes, adding the module "java.management" prevents the output above. No log information is put out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants