diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/bridge/LogEventWrapper.java b/log4j-1.2-api/src/main/java/org/apache/log4j/bridge/LogEventWrapper.java index c0598da6a4d..2591bc5d283 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/bridge/LogEventWrapper.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/bridge/LogEventWrapper.java @@ -20,6 +20,7 @@ import org.apache.log4j.helpers.OptionConverter; import org.apache.log4j.spi.LocationInfo; import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.spi.ThrowableInformation; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.ThreadContext; @@ -36,6 +37,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * Exposes a Log4j 1 logging event as a Log4j 2 LogEvent. @@ -45,17 +47,19 @@ public class LogEventWrapper implements LogEvent { private final LoggingEvent event; private final ContextDataMap contextData; private final MutableThreadContextStack contextStack; - private volatile Thread thread; + private Thread thread; public LogEventWrapper(LoggingEvent event) { this.event = event; this.contextData = new ContextDataMap(event.getProperties()); this.contextStack = new MutableThreadContextStack(NDC.cloneStack()); + this.thread = Objects.equals(event.getThreadName(), Thread.currentThread().getName()) + ? Thread.currentThread() : null; } @Override public LogEvent toImmutable() { - return null; + return this; } @Override @@ -130,12 +134,12 @@ public long getThreadId() { @Override public int getThreadPriority() { - Thread thread = getThread(); - return thread != null ? thread.getPriority() : 0; + Thread thread = getThread(); + return thread != null ? thread.getPriority() : 0; } private Thread getThread() { - if (thread == null) { + if (thread == null && event.getThreadName() != null) { for (Thread thread : Thread.getAllStackTraces().keySet()) { if (thread.getName().equals(event.getThreadName())) { this.thread = thread; @@ -143,15 +147,13 @@ private Thread getThread() { } } } - return null; + return thread; } @Override public Throwable getThrown() { - if (event.getThrowableInformation() != null) { - return event.getThrowableInformation().getThrowable(); - } - return null; + ThrowableInformation throwableInformation = event.getThrowableInformation(); + return throwableInformation == null ? null : throwableInformation.getThrowable(); } @Override diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/bridge/LogEventWrapperTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/bridge/LogEventWrapperTest.java new file mode 100644 index 00000000000..af380457014 --- /dev/null +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/bridge/LogEventWrapperTest.java @@ -0,0 +1,50 @@ +/* + * 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. + */ + +package org.apache.log4j.bridge; + +import org.apache.log4j.spi.LoggingEvent; +import org.apache.logging.log4j.core.LogEvent; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +public class LogEventWrapperTest { + + @Test + public void testThread() { + Thread currentThread = Thread.currentThread(); + String threadName = currentThread.getName(); + LoggingEvent log4j1Event = new LoggingEvent() { + + @Override + public String getThreadName() { + return threadName; + } + }; + LogEvent log4j2Event = new LogEventWrapper(log4j1Event); + assertEquals(currentThread.getId(), log4j2Event.getThreadId()); + assertEquals(currentThread.getPriority(), log4j2Event.getThreadPriority()); + } + + @Test + public void testToImmutable() { + LogEventWrapper wrapper = new LogEventWrapper(new LoggingEvent()); + assertSame(wrapper, wrapper.toImmutable()); + } +} diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3e1f08e5ef4..f77a2809ca7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -98,6 +98,9 @@ Avoid initializing volatile fields with default values. + + Fix log4j-1.2-api LogEventWrapper threadId and priority accessors when called multiple times. +