Log correlation IDs when Micrometer tracing is being used
Add support for logging correlation IDs with Logback or Log4J2 whenever Micrometer tracing is being used. The `LoggingSystemProperties` class now accepts a defualt value resolver which will be used whenever a value isn't in the environment. The `AbstractLoggingSystem` provides a resolver that supports the `logging.pattern.correlation` property and will return a value whenever `LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY` is set. Using `LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY` allows us to provide a consistent width for the correlation ID, even when it's missing from the MDC. The exact correlation pattern returned will depend on the `LoggingSytem` implementation. Currently Logback and Log4J2 are supported and both make use of a custom converter which delegates to a new `CorrelationIdFormatter` class. Closes gh-33280
This commit is contained in:
committed by
Phillip Webb
parent
b6120d504a
commit
c1b295fd71
@@ -65,7 +65,25 @@ Now open the Zipkin UI at `http://localhost:9411` and press the "Run Query" butt
|
||||
You should see one trace.
|
||||
Press the "Show" button to see the details of that trace.
|
||||
|
||||
TIP: You can include the current trace and span id in the logs by setting the `logging.pattern.level` property to `%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]`
|
||||
|
||||
|
||||
[[actuator.micrometer-tracing.logging]]
|
||||
=== Logging Correlation IDs
|
||||
Correlation IDs provide a helpful way to link lines in your log files to distributed traces.
|
||||
By default, as long as configprop:management.tracing.enabled[] has not been set to `false`, Spring Boot will include correlation IDs in your logs whenever you are using Micrometer tracing.
|
||||
|
||||
The default correlation ID is built from `traceId` and `spanId` https://logback.qos.ch/manual/mdc.html[MDC] values.
|
||||
For example, if Micrometer tracing has added an MDC `traceId` of `803B448A0489F84084905D3093480352` and an MDC `spanId` of `3425F23BB2432450` the log output will include the correlation ID `[803B448A0489F84084905D3093480352-3425F23BB2432450]`.
|
||||
|
||||
If you prefer to use a different format for your correlation ID, you can use the configprop:logging.pattern.correlation[] property to define one.
|
||||
For example, the following will provide a correlation ID for Logback in format previously used by Spring Sleuth:
|
||||
|
||||
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
||||
----
|
||||
logging:
|
||||
pattern:
|
||||
correlation: "[${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
|
||||
----
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ The following items are output:
|
||||
* Process ID.
|
||||
* A `---` separator to distinguish the start of actual log messages.
|
||||
* Thread name: Enclosed in square brackets (may be truncated for console output).
|
||||
* Correlation ID: If tracing is enabled (not shown in the sample above)
|
||||
* Logger name: This is usually the source class name (often abbreviated).
|
||||
* The log message.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user