Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions log4j-core-test/src/main/resources/log4j2-calling-class.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -157,4 +159,8 @@ public void log(final Supplier<String> messageSupplier) {
log();
}

@Override
public void setCallerBoundary(String fqcn) {
this.fqcn = fqcn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 1, messages.size());
for (final String message : messages) {
assertEquals("Incorrect fqcn.", "MyFqcn", message);
}
}
}
29 changes: 29 additions & 0 deletions src/changelog/.2.x.x/1533_set_fqcn_eventbuilder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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>