In this tutorial, we will learn how to customize the Spring Boot Console pattern by setting the logging.pattern.console
property in the file application.properties. The simplest way to do it is copying and pasting the default pattern specified in DefaultLogbackConfiguration
class as CONSOLE_LOG_PATTERN
constant and start modifying it. Following is the default pattern:
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint}"
+ "%clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} "
+ "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} "
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
logging.pattern.console
property only works if we use Logback logging implementation. The pattern which is needed to be specified also follows the Logback layout rules as specified here.
Spring additionally provides some custom logback's Converter implementations to customize output, for example Spring binds the convention-word '%clr' to ColorConverter
which returns output with the ANSI color codes. '%wEx' is another conversion word which binds to a converter which returns a custom exception output. Check out DefaultLogbackConfiguration.base()
to learn how Spring does that.
Example Custom Console pattern
Following is a custom Console pattern:
src/main/resources/application.properties
spring.main.banner-mode=off spring.output.ansi.enabled=ALWAYS logging.pattern.console=%clr(%d{yy-MM-dd E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(${PID}){faint} %clr(---){faint} %clr([%8.15t]){cyan} %clr(%-40.40logger{0}){blue} %clr(:){red} %clr(%m){faint}%n
Spring Boot also maps the different log levels to different ANSI color codes as specified here; that only works if we don't specify our own %clr conversion-word value with %p, for example something like %clr(%5p){green}.
Main class
@SpringBootApplication
public class DemoClass {
private static final Logger logger = LoggerFactory.getLogger(DemoClass.class);
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext context =
SpringApplication.run(DemoClass.class, args);
logger.warn("warning msg");
logger.error("error msg");
}
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials