I was recently adding Spring Cloud Sleuth to my Spring Boot applications, and I was wondering how I can make sure that my logging pattern is used but I will also have the sleuth elements in there. I was surprised that this seemed to work like magic, out of the box, nothing big to change for me, except to pull the default logigng pattern from the defaults.xml file, put it into my application.yml file in a nice way, done.
But as a curious person, I asked myself how this was possible. I found the explanation on https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html, under chapter 26.5. Heureka - Spring Cloud Sleuth uses the property "
logging.pattern.level"
to inject its own elements inot the logging pattern by replacing the level item. Sneaky! But it works.
Here's my pattern configuration:
# add the user id to logging:logging: pattern.console: >- %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p})
%clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %8X{User}
%clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n
I just had to add my MDC variable "User" to the default pattern.
No comments:
Post a Comment