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

Provide a programmatic way to enable/disable log4j2 JMX beans creation #2462

Closed
sdavids opened this issue Apr 9, 2024 · 4 comments
Closed
Milestone

Comments

@sdavids
Copy link
Contributor

sdavids commented Apr 9, 2024

JUnit 5 - Disable Log4J JMX beans creation in tests

Spring Boot - Do not create log4j2 JMX beans if spring.jmx.enabled=false

Log4j - Enabling JMX

JMX support is enabled by default [...] To disable JMX completely, and prevent these MBeans from being created, specify system property log4j2.disableJmx to true when you start the Java VM.

private static boolean isJmxDisabled() {
return PropertiesUtil.getProperties().getBooleanProperty(PROPERTY_DISABLE_JMX);
}


For almost all tests the creation of the log4j2 JMX beans is unnecessary.

Spring Boot has disabled their JMX beans creation by default. @SpringBootTest will create log4j2 JMX beans if one uses log4j2 as its logging backend:

configurations {
  implementation {
    exclude(module = "spring-boot-starter-logging")
  }
}

dependencies {
  implementation("org.springframework.boot:spring-boot-starter")
  implementation("org.springframework.boot:spring-boot-starter-log4j2")
  testImplementation("org.springframework.boot:spring-boot-starter-test")
}
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jmx.support.MBeanServerFactoryBean;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
class DemoApplicationTests {

  @Test
  void contextLoads() {
    var factory = new MBeanServerFactoryBean();
    factory.setLocateExistingServerIfPossible(true);
    factory.afterPropertiesSet();
    var server = factory.getObject();
    assertThat(server.getDomains()).doesNotContain("org.apache.logging.log4j2"); // fails
  }
}

Ideally, the creation of the log4j JMX beans would be opt-in but I guess for backward compatibility reasons the default cannot be changed.

It is easy to forget to set the log4j2.disableJmx system property—I would wager most people are not even aware of it.


I suggest adding a programmatic way of enabling/disabling the log4j2 JMX bean creation.

Spring Boot could auto-configure it depending on spring.jmx.enabled.

JUnit5 could disable it by default.

Explicitly setting log4j2.disableJmx would override the programmatic enabling/disabling.

@ppkarwasz
Copy link
Contributor

@sdavids,

Would disabling JMX by default work for you (cf. #1229)? We are open to PRs in that direction.

@sdavids
Copy link
Contributor Author

sdavids commented Apr 9, 2024

Yeah, it would be a good idea from a security standpoint also.

I am not sure about backward compatibility though …

@ppkarwasz
Copy link
Contributor

We accept such behavioral changes in minor releases. Users can always revert it through a system property.

@ppkarwasz
Copy link
Contributor

Fixed by #2468

@ppkarwasz ppkarwasz added this to the 2.24.0 milestone Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants