Create a custom converter
This class converts the well known log levels to a custom format
CustomLogLevelConverter.java
package com.hascode;
public class CustomLogLevelConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
switch (event.getLevel().toInt()) {
case Level.ERROR_INT:
return "ERROR!!!";
case Level.WARN_INT:
return "WARN!!";
case Level.INFO_INT:
return "INFO!";
case Level.TRACE_INT:
return "DEBUG";
default:
return event.getLevel().toString();
}
}
}
Register the converter
The following Logback config includes some defaults and registers our custom converter.
Tip
|
The name used for the conversionWord attribute of the conversionRule is also the placeholder used
in our log format definition later.
|
logback-base.xml
<?xml version="1.0" encoding="UTF-8"?>
<included>
<conversionRule conversionWord="customLevel" converterClass="com.hascode.CustomLogLevelConverter" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
</included>
This config is now included into our logback.xml
which is picked up by spring boot.
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-base.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
[..]
</configuration>
Configure the Log Format
Now we may specify our shiny new log levels in our log format by adding the following config
to our application.properties
:
application.properties
logging.pattern.console=%d{DEFAULT_MICROS} %customLevel[%thread] %-32.32logger{32} %message %ex{full} %n