Package org.springframework.boot.logging
Class CorrelationIdFormatter
java.lang.Object
org.springframework.boot.logging.CorrelationIdFormatter
Utility class that can be used to format a correlation identifier for logging based on
W3C
recommendations.
The formatter can be configured with a comma-separated list of names and the expected
length of their resolved value. Each item should be specified in the form
"<name>(length)"
. For example, "traceId(32),spanId(16)"
specifies the
names "traceId"
and "spanId"
with expected lengths of 32
and
16
respectively.
Correlation IDs are formatted as dash separated strings surrounded in square brackets. Formatted output is always of a fixed width and with trailing space. Dashes are omitted if none of the named items can be resolved.
The following example would return a formatted result of
"[01234567890123456789012345678901-0123456789012345] "
:
CorrelationIdFormatter formatter = CorrelationIdFormatter.of("traceId(32),spanId(16)"); Map<String, String> mdc = Map.of("traceId", "01234567890123456789012345678901", "spanId", "0123456789012345"); return formatter.format(mdc::get);
If of(String)
is called with an empty spec the DEFAULT
formatter will
be used.
- Since:
- 3.2.0
- Author:
- Phillip Webb
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final CorrelationIdFormatter
DefaultCorrelationIdFormatter
. -
Method Summary
Modifier and TypeMethodDescriptionformat
(UnaryOperator<String> resolver) Format a correlation from the values in the given resolver.void
formatTo
(UnaryOperator<String> resolver, Appendable appendable) Format a correlation from the values in the given resolver and append it to the givenAppendable
.static CorrelationIdFormatter
Create a newCorrelationIdFormatter
instance from the given specification.static CorrelationIdFormatter
Create a newCorrelationIdFormatter
instance from the given specification.static CorrelationIdFormatter
of
(Collection<String> spec) Create a newCorrelationIdFormatter
instance from the given specification.toString()
-
Field Details
-
DEFAULT
DefaultCorrelationIdFormatter
.
-
-
Method Details
-
format
Format a correlation from the values in the given resolver.- Parameters:
resolver
- the resolver used to resolve named values- Returns:
- a formatted correlation id
-
formatTo
Format a correlation from the values in the given resolver and append it to the givenAppendable
.- Parameters:
resolver
- the resolver used to resolve named valuesappendable
- the appendable for the formatted correlation id
-
toString
-
of
Create a newCorrelationIdFormatter
instance from the given specification.- Parameters:
spec
- a comma-separated specification- Returns:
- a new
CorrelationIdFormatter
instance
-
of
Create a newCorrelationIdFormatter
instance from the given specification.- Parameters:
spec
- a pre-separated specification- Returns:
- a new
CorrelationIdFormatter
instance
-
of
Create a newCorrelationIdFormatter
instance from the given specification.- Parameters:
spec
- a pre-separated specification- Returns:
- a new
CorrelationIdFormatter
instance
-