Class RequestScopedMdc
java.lang.Object
com.linecorp.armeria.common.logging.RequestScopedMdc
Provides the access to request-scoped
MDC
properties. All properties set via the access methods in
this class are bound to a RequestContext
, unlike the traditional thread-local MDC
properties.
Updating the request-scoped context map
Update the request-scoped context map using put(RequestContext, String, String)
,
putAll(RequestContext, Map)
, remove(RequestContext, String)
and
clear(RequestContext)
:
RequestContext ctx = ...;
RequestScopedMdc.put(ctx, "transactionId", "1234");
RequestScopedMdc.putAll(ctx, Map.of("foo", "1", "bar", "2"));
Transferring thread-local properties
Use copy(RequestContext, String)
or copyAll(RequestContext)
to copy some or all of
thread-local MDC
properties to the request-scoped context map:
RequestContext ctx = ...;
MDC.put("transactionId", "1234");
RequestScopedMdc.copy(ctx, "transactionId");
Retrieving a value from the request-scoped context map
You can explicitly retrieve request-scoped properties using get(RequestContext, String)
or
getAll(RequestContext)
:
RequestContext ctx = ...;
String transactionId = RequestScopedMdc.get(ctx, "transactionId");
RequestScopedMdc
replaces SLF4J's underlying MDCAdapter
implementation so that
MDC.get(String)
and MDC.getCopyOfContextMap()
look into the request-scoped context map
before the thread-local context map:
RequestContext ctx = ...;
RequestScopedMdc.put(ctx, "transactionId", "1234");
try (SafeCloseable ignored = ctx.push()) {
assert MDC.get("transactionId").equals("1234");
// A request-scoped property always gets higher priority:
MDC.put("transactionId", "5678");
assert MDC.get("transactionId").equals("1234");
}
// Now using the thread-local property
// because not in a request scope anymore
assert MDC.get("transactionId").equals("5678");
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
clear(RequestContext ctx)
Unbinds all request-scopedMDC
properties from the specifiedRequestContext
.static void
copy(RequestContext ctx, String key)
Copies the specified thread-localMDC
property to the specifiedRequestContext
.static void
copyAll(RequestContext ctx)
Copies all thread-localMDC
properties to the specifiedRequestContext
.get(RequestContext ctx, String key)
Returns the value of the specified request-scopedMDC
property bound to the specifiedRequestContext
.getAll(RequestContext ctx)
static void
put(RequestContext ctx, String key, @Nullable String value)
Binds the specified request-scopedMDC
property to the specifiedRequestContext
.static void
putAll(RequestContext ctx, Map<String,String> map)
Binds the specified request-scopedMDC
properties to the specifiedRequestContext
.static void
remove(RequestContext ctx, String key)
Unbinds the specified request-scopedMDC
property from the specifiedRequestContext
.
-
Method Details
-
get
Returns the value of the specified request-scopedMDC
property bound to the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
key
- the key of the request-scopedMDC
property- Returns:
- the request-scoped
MDC
property.null
if not found.
-
getAll
- Parameters:
ctx
- theRequestContext
- Returns:
- the
Map
that contains all request-scopedMDC
properties. An emptyMap
if there are no request-scopedMDC
properties.
-
put
Binds the specified request-scopedMDC
property to the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
key
- the key of the request-scopedMDC
propertyvalue
- the value of the request-scopedMDC
property
-
putAll
Binds the specified request-scopedMDC
properties to the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
map
- theMap
that contains the request-scopedMDC
properties
-
copy
Copies the specified thread-localMDC
property to the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
key
- the key of the thread-localMDC
property to copy
-
copyAll
Copies all thread-localMDC
properties to the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
-
remove
Unbinds the specified request-scopedMDC
property from the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
key
- the key of the request-scopedMDC
property to unbind
-
clear
Unbinds all request-scopedMDC
properties from the specifiedRequestContext
.- Parameters:
ctx
- theRequestContext
-