Skip to content

Synchronize Log4jLogEvent-related changes from 2.x #3869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -25,7 +25,6 @@
import org.apache.logging.log4j.core.ReusableLogEvent;
import org.apache.logging.log4j.core.async.InternalAsyncUtil;
import org.apache.logging.log4j.core.impl.ContextDataFactory;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.impl.MementoMessage;
import org.apache.logging.log4j.core.time.Clock;
import org.apache.logging.log4j.core.time.Instant;
Expand Down Expand Up @@ -495,38 +494,4 @@ private void clearContextData() {
}
}
}

/**
* Initializes the specified {@code Log4jLogEvent.Builder} from this {@code RingBufferLogEvent}.
* @param builder the builder whose fields to populate
*/
@Override
public void initializeBuilder(final Log4jLogEvent.Builder builder) {
// If the data is not frozen, make a copy of it.
// TODO: merge with MementoLogEvent#memento
final StringMap oldContextData = this.contextData;
final StringMap contextData;
if (oldContextData != null && !oldContextData.isFrozen()) {
contextData = ContextDataFactory.createContextData(oldContextData);
} else {
contextData = oldContextData;
}
builder.setContextData(contextData) //
.setContextStack(contextStack) //
.setEndOfBatch(endOfBatch) //
.setIncludeLocation(includeLocation) //
.setLevel(getLevel()) // ensure non-null
.setLoggerFqcn(fqcn) //
.setLoggerName(loggerName) //
.setMarker(marker) //
.setMessage(memento()) // ensure non-null & immutable
.setNanoTime(nanoTime) //
.setSource(location) //
.setThreadId(threadId) //
.setThreadName(threadName) //
.setThreadPriority(threadPriority) //
.setThrown(getThrown()) //
.setInstant(instant) //
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
package org.apache.logging.log4j.core.impl;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -326,9 +326,7 @@ public void testEquals() {
different("null fqcn", builder(event).setLoggerFqcn(null), event);

different("different name", builder(event).setLoggerName("different"), event);
assertThrows(
NullPointerException.class,
() -> different("null name", builder(event).setLoggerName(null), event));
different("null name", builder(event).setLoggerName(null), event);

different("different marker", builder(event).setMarker(MarkerManager.getMarker("different")), event);
different("null marker", builder(event).setMarker(null), event);
Expand Down Expand Up @@ -365,6 +363,6 @@ private void different(final String reason, final Log4jLogEvent.Builder builder,
@Test
public void testToString() {
// Throws an NPE in 2.6.2
assertNotNull(Log4jLogEvent.newBuilder().build().toString());
assertDoesNotThrow(() -> new Log4jLogEvent().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.impl.MementoLogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.jspecify.annotations.Nullable;

Expand All @@ -40,6 +41,7 @@ public interface LogEvent {
* Returns an immutable version of this log event, which MAY BE a copy of this event.
*
* @return an immutable version of this log event
* @since 2.8.1
*/
LogEvent toImmutable();

Expand All @@ -48,9 +50,18 @@ public interface LogEvent {
* <p>
* Location information for both events might be computed by this method.
* </p>
* <p>
* <strong>Warning:</strong> If {@code event.getMessage()} is an instance of {@link ReusableMessage}, this method
* remove the parameter references from the original message. Callers should:
* </p>
* <ol>
* <li>Either make sure that the {@code event} will not be used again.</li>
* <li>Or call {@link LogEvent#toImmutable()} before calling this method.</li>
* </ol>
* @since 3.0.0
*/
default LogEvent toMemento() {
return new MementoLogEvent(this);
return new Log4jLogEvent.Builder(this).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.util.StringMap;
Expand Down Expand Up @@ -62,10 +61,4 @@ public interface ReusableLogEvent extends LogEvent {
void setThreadPriority(final int threadPriority);

void setNanoTime(final long nanoTime);

/**
* Initializes the specified {@code Log4jLogEvent.Builder} from this {@code ReusableLogEvent}.
* @param builder the builder whose fields to populate
*/
void initializeBuilder(final Log4jLogEvent.Builder builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.logging.log4j.core.util.Constants;
import org.apache.logging.log4j.message.AsynchronouslyFormattable;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.util.InternalApi;
import org.apache.logging.log4j.util.StackLocatorUtil;
import org.jspecify.annotations.Nullable;

Expand All @@ -31,13 +32,14 @@
* Consider this class private.
* </p>
*/
@InternalApi
public final class InternalAsyncUtil {

private InternalAsyncUtil() {}

/**
* Returns the specified message, with its content frozen unless system property
* {@code log4j.format.msg.async} is true or the message class is annotated with
* {@code log4j.async.formatMessagesInBackground} is true or the message class is annotated with
* {@link AsynchronouslyFormattable}.
*
* @param msg the message object to inspect, modify and return
Expand Down
Loading
Loading