Skip to content
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

Pass key-value properties into events in slf4j-reload4j. #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

josephw
Copy link
Contributor

@josephw josephw commented Feb 5, 2024

I'm aiming to migrate an existing codebase off Log4j as a logging framework. Logging is through SLF4J, but there's also extensive direct use of Log4j's org.apache.log4j.MDC to pass values (other than Strings) through to a Layout that produces structured logging output.

An incremental migration would be significantly easier if I were able to adopt SLF4J's new fluent API without having to simultaneously change the backend logging framework.

This PR makes exactly that change: the key-value properties from the fluent API are merged into the snapshot of the MDC, leaving the existing logging backend with exactly the same data, while the logging calls lose their direct dependency on Log4j.

It's not clear to me whether this approach was considered and rejected, because it conflates the two different types of property, or whether it's suitable as a general purpose change.

Merge MDC and fluent key-value properties together in slf4j-reload4j, so
the fluent API can be used with an appropriate Layout.

Signed-off-by: Joseph Walton <[email protected]>
@josephw josephw force-pushed the merge-key-values-into-reload4j-event-properties branch from 2ac1439 to b1c8d63 Compare May 10, 2024 11:07
@josephw
Copy link
Contributor Author

josephw commented Jun 25, 2024

This approach has worked well: I've incrementally migrated the codebase away from direct use of Log4j 1.x to pure SLF4J 2.x, without losing any existing structured logging, or needing any whole-codebase changes. It's now possible to move to another logging framework without changing the application code that's producing logs.

This is currently implemented with a fork of slf4j-reload4j, which could be dropped if this were applied, but that can go away as soon as the logging framework changes to something currently supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant