ppkarwasz commented on code in PR #2329:
URL: https://github.com/apache/logging-log4j2/pull/2329#discussion_r1525347314


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MementoLogEvent.java:
##########
@@ -72,50 +73,37 @@ public MementoLogEvent(final LogEvent event) {
         if (instant.getEpochMillisecond() == 0 && message instanceof 
TimestampMessage) {
             instant.initFromEpochMilli(((TimestampMessage) 
message).getTimestamp(), 0);
         }
-        contextData = memento(event.getContextData());
+        contextData = mementoOfContextData(event.getContextData());
         contextStack = event.getContextStack();
-        source = includeLocation ? event.getSource() : null;
+        source = includeLocation ? event.getSource() : event.peekSource();
         threadName = event.getThreadName();
         threadId = event.getThreadId();
         threadPriority = event.getThreadPriority();
         thrown = event.getThrown();
         thrownProxy = event.getThrownProxy();
     }
 
-    public MementoLogEvent(final LogEvent event, final boolean 
includeLocation) {
-        loggerFqcn = event.getLoggerFqcn();
-        loggerName = event.getLoggerName();
-        instant.initFrom(event.getInstant());
-        nanoTime = event.getNanoTime();
-        level = event.getLevel();
-        marker = event.getMarker();
-        locationRequired = includeLocation;
-        endOfBatch = event.isEndOfBatch();
-        message = mementoOfMessage(event);
-        if (instant.getEpochMillisecond() == 0 && message instanceof 
TimestampMessage) {
-            instant.initFromEpochMilli(((TimestampMessage) 
message).getTimestamp(), 0);
+    private static ReadOnlyStringMap mementoOfContextData(final 
ReadOnlyStringMap readOnlyMap) {
+        if (readOnlyMap instanceof final StringMap stringMap && 
!stringMap.isFrozen()) {
+            final StringMap data = 
ContextDataFactory.createContextData(readOnlyMap);
+            data.freeze();
+            return data;
         }
-        contextData = memento(event.getContextData());
-        contextStack = event.getContextStack();
-        source = includeLocation ? event.getSource() : null;
-        threadName = event.getThreadName();
-        threadId = event.getThreadId();
-        threadPriority = event.getThreadPriority();
-        thrown = event.getThrown();
-        thrownProxy = event.getThrownProxy();
+        // otherwise immutable
+        return readOnlyMap;
     }
 
-    @Override
-    public LogEvent toImmutable() {
-        return this;
+    private static Message mementoOfMessage(final LogEvent event) {
+        final Message message = event.getMessage();
+        if (message instanceof LoggerNameAwareMessage) {
+            ((LoggerNameAwareMessage) 
message).setLoggerName(event.getLoggerName());
+        }
+        return message instanceof final ReusableMessage reusable ? 
reusable.memento() : message;

Review Comment:
   I started to look at these, but I got such a headache, that I decided to 
split the work in three parts:
   
   - location computation issues (calling `LogEvent#getSource` should prepare a 
log event for a thread jump),
   - thread data computation issues (I guess these should just be created, when 
the log event is created),
   - context data computation issues (probably this should be computed just 
before the jump),
   - message immutability issue (no message is immutable until 
`getFormattedMessage()` is called).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to