From d78df651ca9743e3139deae48b1276b6ccb749aa Mon Sep 17 00:00:00 2001 From: Patrik Ivarsson Date: Fri, 24 Jan 2025 08:55:21 +0100 Subject: [PATCH] Add test verifying log42j updateLoggers is called Signed-off-by: Patrik Ivarsson --- .../binder/logging/Log4j2MetricsTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/logging/Log4j2MetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/logging/Log4j2MetricsTest.java index a2dcdb18da..29d4746b46 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/logging/Log4j2MetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/logging/Log4j2MetricsTest.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.time.Duration; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicInteger; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; @@ -186,6 +187,27 @@ void asyncLogShouldNotBeDuplicated() throws IOException { .until(() -> registry.get("log4j2.events").tags("level", "info").counter().count() == 3); } + // see https://github.com/micrometer-metrics/micrometer/pull/872 + @Test + void shouldTriggerLoggersUpdateOnOpenAndClose() { + LoggerContext context = new LoggerContext("test"); + + AtomicInteger reconfigureCount = new AtomicInteger(); + context.addPropertyChangeListener(event -> { + if (event.getNewValue() instanceof Configuration) { + reconfigureCount.incrementAndGet(); + } + }); + + Log4j2Metrics metrics = new Log4j2Metrics(emptyList(), context); + + assertThat(reconfigureCount.get()).isEqualTo(0); + metrics.bindTo(registry); + assertThat(reconfigureCount.get()).isEqualTo(1); + metrics.close(); + assertThat(reconfigureCount.get()).isEqualTo(2); + } + @Test void metricsFilterIsReused() { LoggerContext loggerContext = new LoggerContext("test");