|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
41 | 41 | import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry;
|
42 | 42 | import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
|
43 | 43 | import org.apache.logging.log4j.jul.Log4jBridgeHandler;
|
| 44 | +import org.apache.logging.log4j.status.StatusListener; |
| 45 | +import org.apache.logging.log4j.status.StatusLogger; |
44 | 46 | import org.apache.logging.log4j.util.PropertiesUtil;
|
45 | 47 | import org.junit.jupiter.api.AfterEach;
|
46 | 48 | import org.junit.jupiter.api.BeforeEach;
|
@@ -469,6 +471,42 @@ void initializeAttachesEnvironmentToLoggerContext() {
|
469 | 471 | assertThat(environment).isSameAs(this.environment);
|
470 | 472 | }
|
471 | 473 |
|
| 474 | + @Test |
| 475 | + void initializeRegisterStatusListenerAndAttachToLoggerContext() { |
| 476 | + this.loggingSystem.beforeInitialize(); |
| 477 | + this.loggingSystem.initialize(this.initializationContext, null, null); |
| 478 | + LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); |
| 479 | + StatusListener statusListener = (StatusListener) loggerContext |
| 480 | + .getObject(Log4J2LoggingSystem.STATUS_LISTENER_KEY); |
| 481 | + assertThat(statusListener).isNotNull(); |
| 482 | + assertThat(StatusLogger.getLogger().getListeners()).contains(statusListener); |
| 483 | + } |
| 484 | + |
| 485 | + @Test |
| 486 | + void statusListenerIsUpdatedUponReinitialization() { |
| 487 | + this.loggingSystem.beforeInitialize(); |
| 488 | + this.loggingSystem.initialize(this.initializationContext, null, null); |
| 489 | + // listener should be registered |
| 490 | + LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); |
| 491 | + StatusListener statusListener = (StatusListener) loggerContext |
| 492 | + .getObject(Log4J2LoggingSystem.STATUS_LISTENER_KEY); |
| 493 | + assertThat(statusListener).isNotNull(); |
| 494 | + assertThat(StatusLogger.getLogger().getListeners()).contains(statusListener); |
| 495 | + |
| 496 | + this.loggingSystem.cleanUp(); |
| 497 | + // listener should be removed from context and StatusLogger |
| 498 | + assertThat(StatusLogger.getLogger().getListeners()).doesNotContain(statusListener); |
| 499 | + assertThat(loggerContext.getObject(Log4J2LoggingSystem.STATUS_LISTENER_KEY)).isNull(); |
| 500 | + |
| 501 | + // a new listener should be registered |
| 502 | + this.loggingSystem.beforeInitialize(); |
| 503 | + this.loggingSystem.initialize(this.initializationContext, null, null); |
| 504 | + StatusListener statusListener1 = (StatusListener) loggerContext |
| 505 | + .getObject(Log4J2LoggingSystem.STATUS_LISTENER_KEY); |
| 506 | + assertThat(statusListener1).isNotNull(); |
| 507 | + assertThat(StatusLogger.getLogger().getListeners()).contains(statusListener1).doesNotContain(statusListener); |
| 508 | + } |
| 509 | + |
472 | 510 | @Test
|
473 | 511 | void initializeAddsSpringEnvironmentPropertySource() {
|
474 | 512 | this.environment.setProperty("spring", "boot");
|
|
0 commit comments