Spring Boot - Customizing Console Logging Format

[Updated: Oct 24, 2017, Created: Oct 23, 2017]

In this example, we will see how to customize console pattern by specifying logging.pattern.console property in application.properties file. The easy way to do that to copy paste the default pattern as specified in DefaultLogbackConfiguration class as CONSOLE_LOG_PATTERN constant and start modifying it. Following is the default pattern (spring-boot 1.5.8.RELEASE):

%clr(%d{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 default). 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

Following is our customized 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 ExampleMain {
  private static final Logger logger = LoggerFactory.getLogger(ExampleMain.class);
  public static void main(String[] args) throws InterruptedException {

      ConfigurableApplicationContext context =
              SpringApplication.run(ExampleMain.class, args);
      logger.warn("warning msg");
      logger.error("error msg");
  }
}

Output

Following output is taken from Intellij:

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.5.8.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.12.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Console logging pattern Example Select All Download
  • boot-logging-pattern-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also