diff --git a/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactory.java b/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactory.java index dcc8c3a6ef09..1cf774c9ced4 100644 --- a/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactory.java +++ b/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactory.java @@ -45,6 +45,7 @@ protected void install(InternalLogger.Factory applicationLoggerFactory) { while (inMemoryLogStore.currentSize() > 0) { inMemoryLogStore.flush(applicationLoggerFactory); } + inMemoryLogStore.setApplicationLoggerFactory(applicationLoggerFactory); // actually install the application logger - from this point, everything will be logged // directly through the application logging system diff --git a/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/InMemoryLogStore.java b/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/InMemoryLogStore.java index 291f9483f5b1..2cfa1570f83b 100644 --- a/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/InMemoryLogStore.java +++ b/javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/InMemoryLogStore.java @@ -20,12 +20,22 @@ final class InMemoryLogStore { private final int limit; + private InternalLogger.Factory applicationLoggerFactory; + InMemoryLogStore(int limit) { this.limit = limit; } void write(InMemoryLog log) { synchronized (lock) { + // redirect to application logging system if it is already set up + // this is here to ensure that we don't lose any logs when switching from in memory to + // application logging + if (applicationLoggerFactory != null) { + applicationLoggerFactory.create(log.name()).log(log.level(), log.message(), log.error()); + return; + } + // just drop the log if hit the limit if (limit >= 0 && inMemoryLogs.size() >= limit) { return; @@ -49,6 +59,12 @@ void flush(InternalLogger.Factory applicationLoggerFactory) { } } + void setApplicationLoggerFactory(InternalLogger.Factory applicationLoggerFactory) { + synchronized (lock) { + this.applicationLoggerFactory = applicationLoggerFactory; + } + } + int currentSize() { synchronized (lock) { return inMemoryLogs.size(); diff --git a/javaagent-internal-logging-application/src/test/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactoryTest.java b/javaagent-internal-logging-application/src/test/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactoryTest.java index 3333cc19ad78..417631a896fe 100644 --- a/javaagent-internal-logging-application/src/test/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactoryTest.java +++ b/javaagent-internal-logging-application/src/test/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactoryTest.java @@ -60,6 +60,7 @@ void shouldOnlyInstallTheFirstBridge() { verify(logStore, times(3)).currentSize(); verify(logStore).flush(applicationLoggerBridge); + verify(logStore).setApplicationLoggerFactory(applicationLoggerBridge); verify(logStore).freeMemory(); underTest.install(applicationLoggerBridge);