Sub topic 8
Customizing the log pattern
This chapter assumes you can write log statements with SLF4J and set levels and output targets in application.properties. You will change what each log line looks like, using Logback's pattern syntax through Spring Boot properties.
What a pattern is
A log pattern is a template string. Logback fills it in for every log event. Plain text is copied as written. A % followed by a name, called a conversion word, is replaced with data from the event, such as the time, level, or message.
Spring Boot exposes two properties:
logging.pattern.consolesets the pattern for console output.logging.pattern.filesets the pattern for the log file.
Setting them separately lets you keep the console short for developers and the file detailed for later analysis.
Common conversion words
| Word | Output |
|---|---|
| `%d{HH:mm:ss.SSS}` | Timestamp in the format you give in braces |
| `%-5level` | Level, left-aligned and padded to 5 characters |
| `%logger{36}` | Logger name, shortened to about 36 characters |
| `%thread` | Name of the thread that logged |
| `%X{requestId}` | A value from the MDC, empty if the key is not set |
| `%msg` | The formatted log message |
| `%n` | Line separator |
| `%ex` | Exception stack trace, appended after the message |
A worked example
Add this to application.properties:
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level [%X{requestId}] %logger{36} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{requestId}] %logger - %msg%nWith the logging code from the earlier chapter, the console line looks like this:
11:08:30.234 INFO [] com.example.orders.OrderService - Placing order A-17 with 3 itemsThe brackets are empty because nothing has put a requestId into the MDC yet. The chapter on MDC fills that gap. The file pattern uses a full date, because a file may hold many days of output, and it adds the thread name, which helps when you read concurrent requests.
Colors and Spring Boot's own words
Spring Boot adds conversion words that plain Logback lacks. The most useful is %clr, which colors a part of the line when the terminal supports ANSI colors:
logging.pattern.console=%clr(%d{HH:mm:ss.SSS}){faint} %clr(%-5level) %clr(%logger{36}){cyan} - %msg%nColor codes in a file are noise, so keep %clr out of logging.pattern.file.
Common errors
- Forgetting
%n. Without it, every event runs onto the same line. - Dropping the exception. The default pattern includes the stack trace through an exception conversion. A custom pattern that omits it still prints stack traces in Logback, because Logback appends
%exautomatically when the pattern has no throwable word. If you use%nopex, you remove it on purpose. - Expensive words. Words such as
%class,%method, and%linecapture caller data, which is slow. Avoid them in production. - Unquoted special characters in YAML. If you use
application.yml, wrap the pattern in quotes, because%at the start of a value is not valid unquoted YAML. - Expecting a pattern to change a setting it does not control. The pattern shapes text only. Levels and destinations are set elsewhere.
Exercise
You want the log file to show the thread name, but the console to stay short. Which properties do you set, and why should the file pattern not use %clr?
Show answer
Set logging.pattern.file with %thread in it, and leave logging.pattern.console without it. %clr writes ANSI color escape codes, which appear as unreadable characters when written to a file.
Exercise
A custom pattern prints [] for %X{requestId} on every line. What is the most likely cause?
Show answer
Nothing has stored a requestId in the MDC for that thread, for example because the filter that sets it is missing or has not run. The pattern is correct, and it prints an empty value when the key is absent.