Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -130,28 +134,26 @@ 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;
return thread;
}
}
}
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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">
Expand Down