You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/07/11 04:18:46 UTC
[logging-log4j2] branch main updated: Allow to override fqcn in Log4jEventBuilder
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/main by this push:
new 9af97bc4f3 Allow to override fqcn in Log4jEventBuilder
9af97bc4f3 is described below
commit 9af97bc4f36fdf7ae50e87849f7eb57b9fa50468
Author: oshai <oh...@gmail.com>
AuthorDate: Mon Jul 3 22:17:53 2023 +0300
Allow to override fqcn in Log4jEventBuilder
Fixes #1533
---
.../src/main/resources/log4j2-calling-class.xml | 6 +++++
.../apache/logging/slf4j/Log4jEventBuilder.java | 10 +++++--
.../logging/slf4j/CallerInformationTest.java | 16 +++++++++++
.../.3.x.x/1533_set_fqcn_eventbuilder.xml | 31 +++++++++-------------
4 files changed, 42 insertions(+), 21 deletions(-)
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 1af22eaa82..d643045cbd 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 @@
<List name="Method">
<PatternLayout pattern="%method"/>
</List>
+ <List name="Fqcn">
+ <PatternLayout pattern="%fqcn"/>
+ </List>
</Appenders>
<Loggers>
<Logger name="ClassLogger" level="info">
@@ -31,6 +34,9 @@
<Logger name="MethodLogger" level="info">
<AppenderRef ref="Method"/>
</Logger>
+ <Logger name="FqcnLogger" level="info">
+ <AppenderRef ref="Fqcn"/>
+ </Logger>
<Root level="off"/>
</Loggers>
</Configuration>
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 bce6ffdd15..33a5b8dca4 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.Level;
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<String, String> 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 class Log4jEventBuilder implements LoggingEventBuilder {
.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 class Log4jEventBuilder implements LoggingEventBuilder {
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 a24d30a9db..06d28bbbbf 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.ClassRule;
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 class CallerInformationTest {
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<String> messages = app.getMessages();
+ assertEquals("Incorrect number of messages.", 1, messages.size());
+ for (final String message : messages) {
+ assertEquals("Incorrect fqcn.", "MyFqcn", message);
+ }
+ }
}
diff --git a/log4j-core-test/src/main/resources/log4j2-calling-class.xml b/src/changelog/.3.x.x/1533_set_fqcn_eventbuilder.xml
similarity index 59%
copy from log4j-core-test/src/main/resources/log4j2-calling-class.xml
copy to src/changelog/.3.x.x/1533_set_fqcn_eventbuilder.xml
index 1af22eaa82..221f0a6f53 100644
--- a/log4j-core-test/src/main/resources/log4j2-calling-class.xml
+++ b/src/changelog/.3.x.x/1533_set_fqcn_eventbuilder.xml
@@ -15,22 +15,15 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
-<Configuration name="CallerInformationTest" status="ERROR">
- <Appenders>
- <List name="Class">
- <PatternLayout pattern="%class"/>
- </List>
- <List name="Method">
- <PatternLayout pattern="%method"/>
- </List>
- </Appenders>
- <Loggers>
- <Logger name="ClassLogger" level="info">
- <AppenderRef ref="Class"/>
- </Logger>
- <Logger name="MethodLogger" level="info">
- <AppenderRef ref="Method"/>
- </Logger>
- <Root level="off"/>
- </Loggers>
-</Configuration>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://logging.apache.org/log4j/changelog"
+ xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
+ type="fixed">
+ <issue id="1533" link="https://github.com/apache/logging-log4j2/pull/1533"/>
+ <author id="github:oshai" name="Ohad Shai"/>
+ <!-- Committer -->
+ <author name="github:pkarwasz"/>
+ <description format="asciidoc">
+ Allow to override fqcn in `Log4jEventBuilder` by implementing `CallerBoundaryAware`.
+ </description>
+</entry>