sdavids opened a new issue, #2462:
URL: https://github.com/apache/logging-log4j2/issues/2462

   [JUnit 5 - Disable Log4J JMX beans creation in 
tests](https://github.com/junit-team/junit5/pull/3753#issuecomment-2045573225)
   
   [Spring Boot - Do not create log4j2 JMX beans if 
spring.jmx.enabled=false](https://github.com/spring-projects/spring-boot/issues/40273)
   
   [Log4j - Enabling 
JMX](https://logging.apache.org/log4j/2.x/manual/jmx.html#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.
   
   
https://github.com/apache/logging-log4j2/blob/7b5d23e1403db7a7865da4783dd5cb22b0ddfb93/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java#L129-L131
   
   ---
   
   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:
   
   ```kotlin
   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")
   }
   ```
   
   ```java
   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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to