Class CorrelationIdFormatter

java.lang.Object
org.springframework.boot.logging.CorrelationIdFormatter

public final class CorrelationIdFormatter extends Object
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: