Improve implementations of LogEvent.toImmutable() and ReusableMessage.memento()#3171
Merged
Improve implementations of LogEvent.toImmutable() and ReusableMessage.memento()#3171
LogEvent.toImmutable() and ReusableMessage.memento()#3171Conversation
The implementations of `LogEvent.toImmutable()` either use serialization or one of the `Log4jLogEvent.Builder` constructors and custom per class code. This PR: - Redirects all implementation of `LogEvent.toImmutable` to the `Builder(LogEvent)` constructor (except `Log4jLogEvent.toImmutable()`) - Improve the constructor to create really immutable and thread-safe instances. - Removes the usage of `ThrowableProxy` for purposes not related to serialization. - Fixes some implementations of `ReusableMessage.memento()`. - Uses `ReusableMessage.memento()` instead of `ImmutableMessage` where applicable.
ppkarwasz
commented
Nov 4, 2024
…hrowable-proxy-clean-up
vy
approved these changes
May 3, 2025
Adds comments to explain the side effects of the `getFormattedMessage` calls.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR improves the implementations of LogEvent.toImmutable() and ReusableMessage.memento() by unifying the immutable conversion through the Log4jLogEvent.Builder, removing deprecated and unnecessary thrownProxy usages, and ensuring that messages are properly cached in their immutable forms.
- Redirects all implementations of LogEvent.toImmutable() to use the Builder(LogEvent) constructor
- Updates memento() implementations in reusable messages to cache their formatted strings
- Removes thrownProxy usage from non‐serialization contexts and adapts tests accordingly
Reviewed Changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| log4j-core/impl/ReusableLogEventFactory.java | Updates in Javadoc and thread info retrieval to clarify message mutation semantics. |
| log4j-core/impl/MutableLogEvent.java | Replaces createMemento() with toImmutable() and removes thrownProxy references. |
| log4j-core/impl/Log4jLogEvent.java | Refactors immutability, lazy initialization, and thrownProxy handling; adjusts equals/hashCode. |
| log4j-core/config/LoggerConfig.java | Removes deprecated LogEvent instantiation in property evaluation. |
| log4j-core/async/* | Uses toImmutable() consistently for converting mutable events. |
| log4j-api/message/* | Updates memento() implementations to cache formatted messages. |
| log4j-1.2-api/rewrite/* | Simplifies event rewrite by removing thrownProxy setting. |
| Test files | Update assertions to check for immutable messages instead of legacy memento types. |
Comments suppressed due to low confidence (1)
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java:1091
- [nitpick] Removing thrownProxy from the equals and hashCode methods alters the equality semantics; please verify that this change does not break any contract or expected behavior.
if (thrownProxy != null ? !thrownProxy.equals(that.thrownProxy) : that.thrownProxy != null) {
Apply Copilot suggestion about race conditions: #3171 (comment)
This reverts commit 1455a17.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The implementations of
LogEvent.toImmutable()either use serialization or one of theLog4jLogEvent.Builderconstructors and custom per class code.This PR:
LogEvent.toImmutableto theBuilder(LogEvent)constructor (exceptLog4jLogEvent.toImmutable())ThrowableProxyfor purposes not related to serialization.ReusableMessage.memento().ReusableMessage.memento()instead ofImmutableMessagewhere applicable.