-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: main
Are you sure you want to change the base?
Conversation
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.
@ppkarwasz, you authored #3171, which this PR originates from. I'd appreciate it if you can help with the review. |
I'll review this PR later this week, but please note this is a challenging port. The changes introduced in #3171 primarily address backward compatibility. The concept of "mutability" in version 2.x has been quite confusing, and I believe we have an opportunity to improve clarity significantly in 3.x. Specifically:
These are preliminary thoughts, and since I haven't reviewed the changes thoroughly yet, some of these ideas might already be implemented. |
The naming overlap with similar but not equal method names is related to what you identified here (classes implementing both
That sounds a little bit of an internal detail considering these checks are largely relevant to when an object is returned a pool (which is how we got rid of the frozen flag in some reusable class after adding
I think with the changes you made such that
I did remove an extra |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jvz,
Thanks for syncing these changes from the 2.x
branch. The PR looks faithful to those updates, but I think a few customizations could help it align better with the current 3.x
codebase. Let me know if you’d prefer to address them here or in follow-up PRs.
-
toMemento()
behavior
In3.x
, the newtoMemento()
method is documented as removing parameter references from the original message, but as far as I can tell it just callsMessage.memento()
orMessage.getFormattedMessage()
. It is also effectively a synonym fortoImmutable()
, so the docs suggesting otherwise seem misleading. -
Event factory differences
SinceDefaultLogEventFactory
no longer exists in3.x
, all new log events areMutableLogEvent
instances.Log4jLogEvent
only appears as a snapshot, andLog4jLogEvent.newBuilder()
is only used in tests (with one exception inLoggerConfig
). We could consider removing it together with the public no-argBuilder
constructor. -
Builder initialization
Given the above,Log4jLogEvent.Builder
no longer needsClock
andContextDataInjector
: those responsibilities now live inReusableLogEventFactory
. -
Timestamp handling
TheLog4jLogEvent.Builder
has logic for initializing timestamps withTimestampMessage
. That logic seems to be missing fromReusableLogEventFactory
. -
Lazy initialization
With Java 17 as the baseline, I don’t see a strong reason forgetThreadName()
(and similar methods) to remain lazy. -
Making
Builder
more flexible
It could be useful to makeLog4jLogEvent.Builder
more universal by letting the caller decide:- Whether to call
getSource()
and possibly compute the location - Whether to call
getFormattedMessage()
on the copied message - Whether to initialize the context map
- Whether to initialize the timestamp
- Whether to call
If you agree with some of these, feel free to tackle them here or open follow-ups against main
, whichever makes more sense.
* <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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't toMemento()
a synonym of toImmutable()
now? The suggestion of calling toImmutable()
before calling this method is misleading.
*/ | ||
public Builder(final LogEvent other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: instead of a public constructor, we could have a newBuilder(LogEvent)
factory method.
This updates
Log4jLogEvent
and its builder class to work similarly to what is in 2.25.0. The previously addedMementoLogEvent
class can be removed as the builder can make memento copies better.This ports changes introduced in #3171 which seems somewhat inspired by changes made in
main
previously. However, due to the changes inLog4jLogEvent
, this removes the need for the added memento class.