From 45063f98b7fad98d5e30a7b290e23bbaed1ff1ae Mon Sep 17 00:00:00 2001 From: oshai Date: Mon, 3 Jul 2023 22:17:53 +0300 Subject: [PATCH] Allow to override fqcn in Log4jEventBuilder Fixes #1533 --- .../main/resources/log4j2-calling-class.xml | 6 ++++ .../logging/slf4j/Log4jEventBuilder.java | 10 +++++-- .../logging/slf4j/CallerInformationTest.java | 16 ++++++++++ .../.2.x.x/1533_set_fqcn_eventbuilder.xml | 29 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/changelog/.2.x.x/1533_set_fqcn_eventbuilder.xml diff --git a/log4j-core-test/src/main/resources/log4j2-calling-class.xml b/log4j-core-test/src/main/resources/log4j2-calling-class.xml index 9f6e8605b18..c44e6903460 100644 --- a/log4j-core-test/src/main/resources/log4j2-calling-class.xml +++ b/log4j-core-test/src/main/resources/log4j2-calling-class.xml @@ -23,6 +23,9 @@ + + + @@ -31,6 +34,9 @@ + + + diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java index bce6ffdd150..33a5b8dca4d 100644 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java +++ b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java @@ -29,9 +29,10 @@ import org.apache.logging.log4j.LogBuilder; import org.apache.logging.log4j.Logger; import org.slf4j.Marker; +import org.slf4j.spi.CallerBoundaryAware; import org.slf4j.spi.LoggingEventBuilder; -public class Log4jEventBuilder implements LoggingEventBuilder { +public class Log4jEventBuilder implements LoggingEventBuilder, CallerBoundaryAware { private static final String FQCN = Log4jEventBuilder.class.getName(); @@ -43,6 +44,7 @@ public class Log4jEventBuilder implements LoggingEventBuilder { private Throwable throwable = null; private Map keyValuePairs = null; private final Level level; + private String fqcn = FQCN; public Log4jEventBuilder(final Log4jMarkerFactory markerFactory, final Logger logger, final Level level) { this.markerFactory = markerFactory; @@ -110,7 +112,7 @@ public void log() { .withMarker(marker) .withThrowable(throwable); if (logBuilder instanceof BridgeAware) { - ((BridgeAware) logBuilder).setEntryPoint(FQCN); + ((BridgeAware) logBuilder).setEntryPoint(fqcn); } if (keyValuePairs == null || keyValuePairs.isEmpty()) { logBuilder.log(message, arguments.toArray()); @@ -157,4 +159,8 @@ public void log(final Supplier messageSupplier) { log(); } + @Override + public void setCallerBoundary(String fqcn) { + this.fqcn = fqcn; + } } diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java index a24d30a9db5..06d28bbbbf8 100644 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java +++ b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java @@ -24,6 +24,8 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.spi.CallerBoundaryAware; +import org.slf4j.spi.LoggingEventBuilder; import static org.junit.Assert.assertEquals; @@ -72,4 +74,18 @@ public void testMethodLogger() throws Exception { assertEquals("Incorrect caller method name.", "testMethodLogger", message); } } + + @Test + public void testFqcnLogger() throws Exception { + final ListAppender app = ctx.getListAppender("Fqcn").clear(); + final Logger logger = LoggerFactory.getLogger("FqcnLogger"); + LoggingEventBuilder loggingEventBuilder = logger.atInfo(); + ((CallerBoundaryAware)loggingEventBuilder).setCallerBoundary("MyFqcn"); + loggingEventBuilder.log("A message"); + final List messages = app.getMessages(); + assertEquals("Incorrect number of messages.", 1, messages.size()); + for (final String message : messages) { + assertEquals("Incorrect fqcn.", "MyFqcn", message); + } + } } diff --git a/src/changelog/.2.x.x/1533_set_fqcn_eventbuilder.xml b/src/changelog/.2.x.x/1533_set_fqcn_eventbuilder.xml new file mode 100644 index 00000000000..221f0a6f539 --- /dev/null +++ b/src/changelog/.2.x.x/1533_set_fqcn_eventbuilder.xml @@ -0,0 +1,29 @@ + + + + + + + + + Allow to override fqcn in `Log4jEventBuilder` by implementing `CallerBoundaryAware`. + +