I’m pleased to announce that we have now released new versions of the spt-development-*-spring-boot projects for integrating the spt-development libraries related to correlation IDs, logging and auditing, into Spring Boot applications. These new releases along with new releases of some of the projects they relate to, have been updated to integrate with the slf4j MDC context. Providing a simpler way of including the correlation ID in your logs. As always, the source is available on GitHub and the artifacts are also available in Maven Central for easy inclusion in your own Java projects.

The following projects have all been updated to v2.0.10:

Additionally, the following libraries have also been updated:

We have also upgraded the demo project to v2.0.10 of all the libraries above.

Mapped Diagnostic Context

Simply adding v2.0.10 spt-development-cid-web-spring-boot-starter or spt-development-cid-jms-spring-boot-starter (depending on your requirements) as a dependency to your project, will be enough to have the correlation ID added to the Mapped Diagnostic Context of the underlying logging system you are using. The correlation ID will be added with slf4j’s MDC class, for each new HTTP request or JMS message received. In order for the correlation ID to appear in your logs, you will need to update your logging pattern to include a reference to the MDC key as shown in a section of application.yml taken from the demo, project below.

Here the default key of cid is being used. You will also notice the use of the replace function; replace is being used so that the correlation ID is only output when set, so that any logging output by Spring Boot before the request filter or JMS listener fires, is output without the correlation ID. NOTE the default key can be customised with the spt.cid.mdc.cid-key property.

If you are upgrading from v2.0.9 and you have followed the suggested approach of including the correlation ID in all of your application’s log statements demonstrated in this post, you will need to remove the CorrelationId.get() call or accept that you will have the correlation ID appear twice each for each entry in your logs. This changeset shows the changes made to the demo project to remove the explicit inclusion of correlation IDs in the logs. On the plus side, if you are using spt-development-logging-spring-boot, the number of changes will be reduced.

Disable MDC

Although it is the default functionality and recommended approach, if you don’t want to use MDC, maybe because you already have CorrelationId.get() calls in log statements scattered throughout your code, then it it is possible to disable this new functionality by setting the spt.cid.mdc.disabled property to true. The demo project build has been extended to run with a new mdc-disabled profile to prove that the libraries function correctly when this property is set. This is purely for demonstration and testing purposes however and not expected to be used to switch this functionality on and off in production, although there is no reason why it couldn’t be.

Why the U-turn

It was a conscious decision early on not to use the MDC. However, having had feedback from various users, it became eveident that this was a feature that was lacking in the library and having to explicitly add CorrelationId.get() explicitly to log statements was putting some people off from using the library.

Integration with Spring Cloud Sleuth Micrometer is on the road map and the use of MDC is expected to make that integration simpler in the future.

[EDIT] There is now a post describing how to integrate the logging and auditing libraries with Micrometer.


We hope this helps the community, and if there are any questions or comments, please don’t hesitate to get in touch.