Skip to content

Commit bff263e

Browse files
committed
Synchronize Log4jLogEvent-related changes from 2.x
This updates `Log4jLogEvent` and its builder class to work similarly to what is in 2.25.0. The previously added `MementoLogEvent` class can be removed as the builder can make memento copies better.
1 parent ebfc571 commit bff263e

File tree

9 files changed

+175
-449
lines changed

9 files changed

+175
-449
lines changed

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/RingBufferLogEvent.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.apache.logging.log4j.core.ReusableLogEvent;
2626
import org.apache.logging.log4j.core.async.InternalAsyncUtil;
2727
import org.apache.logging.log4j.core.impl.ContextDataFactory;
28-
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2928
import org.apache.logging.log4j.core.impl.MementoMessage;
3029
import org.apache.logging.log4j.core.time.Clock;
3130
import org.apache.logging.log4j.core.time.Instant;
@@ -495,38 +494,4 @@ private void clearContextData() {
495494
}
496495
}
497496
}
498-
499-
/**
500-
* Initializes the specified {@code Log4jLogEvent.Builder} from this {@code RingBufferLogEvent}.
501-
* @param builder the builder whose fields to populate
502-
*/
503-
@Override
504-
public void initializeBuilder(final Log4jLogEvent.Builder builder) {
505-
// If the data is not frozen, make a copy of it.
506-
// TODO: merge with MementoLogEvent#memento
507-
final StringMap oldContextData = this.contextData;
508-
final StringMap contextData;
509-
if (oldContextData != null && !oldContextData.isFrozen()) {
510-
contextData = ContextDataFactory.createContextData(oldContextData);
511-
} else {
512-
contextData = oldContextData;
513-
}
514-
builder.setContextData(contextData) //
515-
.setContextStack(contextStack) //
516-
.setEndOfBatch(endOfBatch) //
517-
.setIncludeLocation(includeLocation) //
518-
.setLevel(getLevel()) // ensure non-null
519-
.setLoggerFqcn(fqcn) //
520-
.setLoggerName(loggerName) //
521-
.setMarker(marker) //
522-
.setMessage(memento()) // ensure non-null & immutable
523-
.setNanoTime(nanoTime) //
524-
.setSource(location) //
525-
.setThreadId(threadId) //
526-
.setThreadName(threadName) //
527-
.setThreadPriority(threadPriority) //
528-
.setThrown(getThrown()) //
529-
.setInstant(instant) //
530-
;
531-
}
532497
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package org.apache.logging.log4j.core.impl;
1818

19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22-
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertNull;
2424
import static org.junit.jupiter.api.Assertions.assertSame;
2525
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -326,9 +326,7 @@ public void testEquals() {
326326
different("null fqcn", builder(event).setLoggerFqcn(null), event);
327327

328328
different("different name", builder(event).setLoggerName("different"), event);
329-
assertThrows(
330-
NullPointerException.class,
331-
() -> different("null name", builder(event).setLoggerName(null), event));
329+
different("null name", builder(event).setLoggerName(null), event);
332330

333331
different("different marker", builder(event).setMarker(MarkerManager.getMarker("different")), event);
334332
different("null marker", builder(event).setMarker(null), event);
@@ -365,6 +363,6 @@ private void different(final String reason, final Log4jLogEvent.Builder builder,
365363
@Test
366364
public void testToString() {
367365
// Throws an NPE in 2.6.2
368-
assertNotNull(Log4jLogEvent.newBuilder().build().toString());
366+
assertDoesNotThrow(() -> new Log4jLogEvent().toString());
369367
}
370368
}

log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import org.apache.logging.log4j.Level;
2020
import org.apache.logging.log4j.Marker;
2121
import org.apache.logging.log4j.ThreadContext;
22-
import org.apache.logging.log4j.core.impl.MementoLogEvent;
22+
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2323
import org.apache.logging.log4j.core.time.Instant;
2424
import org.apache.logging.log4j.message.Message;
25+
import org.apache.logging.log4j.message.ReusableMessage;
2526
import org.apache.logging.log4j.util.ReadOnlyStringMap;
2627
import org.jspecify.annotations.Nullable;
2728

@@ -40,6 +41,7 @@ public interface LogEvent {
4041
* Returns an immutable version of this log event, which MAY BE a copy of this event.
4142
*
4243
* @return an immutable version of this log event
44+
* @since 2.8.1
4345
*/
4446
LogEvent toImmutable();
4547

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

5667
/**

log4j-core/src/main/java/org/apache/logging/log4j/core/ReusableLogEvent.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.logging.log4j.Level;
2020
import org.apache.logging.log4j.Marker;
2121
import org.apache.logging.log4j.ThreadContext;
22-
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2322
import org.apache.logging.log4j.core.time.Instant;
2423
import org.apache.logging.log4j.message.Message;
2524
import org.apache.logging.log4j.util.StringMap;
@@ -62,10 +61,4 @@ public interface ReusableLogEvent extends LogEvent {
6261
void setThreadPriority(final int threadPriority);
6362

6463
void setNanoTime(final long nanoTime);
65-
66-
/**
67-
* Initializes the specified {@code Log4jLogEvent.Builder} from this {@code ReusableLogEvent}.
68-
* @param builder the builder whose fields to populate
69-
*/
70-
void initializeBuilder(final Log4jLogEvent.Builder builder);
7164
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/InternalAsyncUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.logging.log4j.core.util.Constants;
2323
import org.apache.logging.log4j.message.AsynchronouslyFormattable;
2424
import org.apache.logging.log4j.message.Message;
25+
import org.apache.logging.log4j.util.InternalApi;
2526
import org.apache.logging.log4j.util.StackLocatorUtil;
2627
import org.jspecify.annotations.Nullable;
2728

@@ -31,13 +32,14 @@
3132
* Consider this class private.
3233
* </p>
3334
*/
35+
@InternalApi
3436
public final class InternalAsyncUtil {
3537

3638
private InternalAsyncUtil() {}
3739

3840
/**
3941
* Returns the specified message, with its content frozen unless system property
40-
* {@code log4j.format.msg.async} is true or the message class is annotated with
42+
* {@code log4j.async.formatMessagesInBackground} is true or the message class is annotated with
4143
* {@link AsynchronouslyFormattable}.
4244
*
4345
* @param msg the message object to inspect, modify and return

0 commit comments

Comments
 (0)