Skip to content

Commit 372ea7c

Browse files
oshaippkarwasz
authored andcommitted
Allow to override fqcn in Log4jEventBuilder
Fixes #1533
1 parent 0a98e38 commit 372ea7c

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

log4j-core-test/src/main/resources/log4j2-calling-class.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<List name="Method">
2424
<PatternLayout pattern="%method"/>
2525
</List>
26+
<List name="Fqcn">
27+
<PatternLayout pattern="%fqcn"/>
28+
</List>
2629
</Appenders>
2730
<Loggers>
2831
<Logger name="ClassLogger" level="info">
@@ -31,6 +34,9 @@
3134
<Logger name="MethodLogger" level="info">
3235
<AppenderRef ref="Method"/>
3336
</Logger>
37+
<Logger name="FqcnLogger" level="info">
38+
<AppenderRef ref="Fqcn"/>
39+
</Logger>
3440
<Root level="off"/>
3541
</Loggers>
3642
</Configuration>

log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
import org.apache.logging.log4j.LogBuilder;
3030
import org.apache.logging.log4j.Logger;
3131
import org.slf4j.Marker;
32+
import org.slf4j.spi.CallerBoundaryAware;
3233
import org.slf4j.spi.LoggingEventBuilder;
3334

34-
public class Log4jEventBuilder implements LoggingEventBuilder {
35+
public class Log4jEventBuilder implements LoggingEventBuilder, CallerBoundaryAware {
3536

3637
private static final String FQCN = Log4jEventBuilder.class.getName();
3738

@@ -43,6 +44,7 @@ public class Log4jEventBuilder implements LoggingEventBuilder {
4344
private Throwable throwable = null;
4445
private Map<String, String> keyValuePairs = null;
4546
private final Level level;
47+
private String fqcn = FQCN;
4648

4749
public Log4jEventBuilder(final Log4jMarkerFactory markerFactory, final Logger logger, final Level level) {
4850
this.markerFactory = markerFactory;
@@ -110,7 +112,7 @@ public void log() {
110112
.withMarker(marker)
111113
.withThrowable(throwable);
112114
if (logBuilder instanceof BridgeAware) {
113-
((BridgeAware) logBuilder).setEntryPoint(FQCN);
115+
((BridgeAware) logBuilder).setEntryPoint(fqcn);
114116
}
115117
if (keyValuePairs == null || keyValuePairs.isEmpty()) {
116118
logBuilder.log(message, arguments.toArray());
@@ -157,4 +159,8 @@ public void log(final Supplier<String> messageSupplier) {
157159
log();
158160
}
159161

162+
@Override
163+
public void setCallerBoundary(String fqcn) {
164+
this.fqcn = fqcn;
165+
}
160166
}

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.junit.Test;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
27+
import org.slf4j.spi.CallerBoundaryAware;
28+
import org.slf4j.spi.LoggingEventBuilder;
2729

2830
import static org.junit.Assert.assertEquals;
2931

@@ -72,4 +74,18 @@ public void testMethodLogger() throws Exception {
7274
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
7375
}
7476
}
77+
78+
@Test
79+
public void testFqcnLogger() throws Exception {
80+
final ListAppender app = ctx.getListAppender("Fqcn").clear();
81+
final Logger logger = LoggerFactory.getLogger("FqcnLogger");
82+
LoggingEventBuilder loggingEventBuilder = logger.atInfo();
83+
((CallerBoundaryAware)loggingEventBuilder).setCallerBoundary("MyFqcn");
84+
loggingEventBuilder.log("A message");
85+
final List<String> messages = app.getMessages();
86+
assertEquals("Incorrect number of messages.", 1, messages.size());
87+
for (final String message : messages) {
88+
assertEquals("Incorrect fqcn.", "MyFqcn", message);
89+
}
90+
}
7591
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
21+
type="fixed">
22+
<issue id="1533" link="https://github.com/apache/logging-log4j2/pull/1533"/>
23+
<author id="github:oshai" name="Ohad Shai"/>
24+
<!-- Committer -->
25+
<author name="github:pkarwasz"/>
26+
<description format="asciidoc">
27+
Allow to override fqcn in `Log4jEventBuilder` by implementing `CallerBoundaryAware`.
28+
</description>
29+
</entry>

0 commit comments

Comments
 (0)