Reactor Contextual Logging is a small Java library designed to assist with logging Reactor Contexts. This library enhances logging capabilities within Reactor by appending the current Context as key-value pairs to log messages. It also includes an optional feature for automatic context propagation to the MDC (Mapped Diagnostic Context) on every operator call.
This library includes methods such as logOnNext(), logOnSubscribe(), logOnCancel() and more that integrate with Flux and Mono.
They allow you to pass a log message and arguments, and will append the current Context to your log statement.
To get started, use Lombok's @ExtensionMethod to integrate these methods into Mono and Flux. You may then use these methods in your Reactor pipeline.
@ExtensionMethod(PublisherExtensions.class)
public class Example {
public static void main(String[] args) {
Flux.range(0, 2)
.logOnNext(INFO, "message")
.contextWrite(Context.of("context_key", "context_value"))
.subscribeOn(Schedulers.boundedElastic()).blockLast();
}
}This library includes an optional feature that allows automatic propagation of Reactor's Context to the MDC (Mapped Diagnostic Context). It lets you write log messages without using the above methods, and within any Reactor operator, and still have the Context appear in your log messages.
It uses Reactor's Hooks.enableAutomaticContextPropagation() internally.
Using this feature is discouraged as it greatly impacts performance, due to the burden of having to write and remove from the MDC on every operator call.
You may enable this feature in your Spring project by specifying fields to be automatically propagated in logging.propagate-context.fields:
logging:
propagate-context:
fields: request_id, item_idIn a vanilla Java project, you can instead call ContextPropagator.propagate(String... fields).