You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2020/07/27 22:48:31 UTC

[logging-log4j2] branch master updated: LOG4J2-2899: Fix log4j-1.2-api LogEventWrapper thread data accessors

This is an automated email from the ASF dual-hosted git repository.

ckozak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 23e8572  LOG4J2-2899: Fix log4j-1.2-api LogEventWrapper thread data accessors
23e8572 is described below

commit 23e8572b8a52504e318bf925e34381edbbf3c1a3
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Sat Jul 18 13:33:24 2020 -0400

    LOG4J2-2899: Fix log4j-1.2-api LogEventWrapper thread data accessors
---
 .../org/apache/log4j/bridge/LogEventWrapper.java   | 22 +++++-----
 .../apache/log4j/bridge/LogEventWrapperTest.java   | 50 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 3 files changed, 65 insertions(+), 10 deletions(-)

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 1e46e12..4ab9db9 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.NDC;
 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 org.apache.logging.log4j.util.TriConsumer;
 
 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
@@ -125,12 +129,12 @@ public class LogEventWrapper implements LogEvent {
 
     @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;
@@ -138,15 +142,13 @@ public class LogEventWrapper implements LogEvent {
                 }
             }
         }
-        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 0000000..af38045
--- /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 516b71c..0f0a246 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -213,6 +213,9 @@
       <action issue="LOG4J2-2898" dev="ckozak" type="fix" due-to="Turbanov Andrey">
         Avoid initializing volatile fields with default values.
       </action>
+      <action issue="LOG4J2-2899" dev="ckozak" type="fix">
+        Fix log4j-1.2-api LogEventWrapper threadId and priority accessors when called multiple times.
+      </action>
     </release>
     <release version="2.13.3" date="2020-05-10" description="GA Release 2.13.3">
       <action issue="LOG4J2-2838" dev="rgoers" type="fix">