Spring Boot logging with application.properties

Spring Boot provides a powerful and flexible logging API. In this post, we will see how to enable Spring Boot logging with application.properties.

 

Introduction

Spring allows to set and configure the logger levels using application.properties file. In this article, we will see how to use application.properties file to configure logging for your Spring Boot application. Here is a sample configuration for Spring Boot logging with application.properties file.

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

 

1. Default Logging Configuration

Let’s run the following code with no logging configuration

package com.javadevjournal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoLoggingController {
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    @GetMapping(value = "/")
    public String test(){

        LOG.debug("debug message");
        LOG.info("This is an info message");
        LOG.warn("Warning for this application");
        LOG.error("Seems error in the application");
        return "test";
    }
}

if you run this program, you may have following output in the console:

2019-01-16 17:48:01.049  INFO 60674 --- [nio-8080-exec-1] com.javadevjournal.Application           : This is an info message
2019-01-16 17:48:01.049  WARN 60674 --- [nio-8080-exec-1] com.javadevjournal.Application           : Warning for this application
2019-01-16 17:48:01.049 ERROR 60674 --- [nio-8080-exec-1] com.javadevjournal.Application           : Seems error in the application

Default logging level is INFO. Let’s inspect this pattern before we customize it:

  • Date and Time: Millisecond precision and easily sortable.
  • Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
  • Process ID.
  • A --- separator to distinguish the start of actual log messages.
  • Thread name: Enclosed in square brackets (maybe truncated for console output).
  • Logger name: This is commonly the source class name (often abbreviated).
  • The log message.

Let’s see how control and customize Spring Boot logging file.

 

2. Log Levels

We can use one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF to control the log level for our application. Let’s take an example where we want to set the log level for our custom package to the WARN. We can use the ‘logging.level.*=LEVEL pattern in our application.properties file for this:

#logging.level.root=WARN
logging.level.com.javadevjournal = WARN
logging.level.org.springframework.web = DEBUG

If we run our above example again with these changes in the application.properties file, we will see following output in the console:

2019-01-16 17:48:51.415  WARN 60674 --- [nio-8080-exec-4] com.javadevjournal.Application           : Warning for this application
2019-01-16 17:48:51.415 ERROR 60674 --- [nio-8080-exec-4] com.javadevjournal.Application           : Seems error in the application

 

3. Spring Boot – Logging Patterns

Spring Boot properties file provide support for logging patterns. To provide logging patterns, use the following two properties in your application.properties file:

logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup.
logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup.

Here is a sample format for your reference:

# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
logging.pattern.file= "%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n"

 

4. Log Output

To log in the file system, we can use following property in our application.properties file:

logging.file= # Log file name (for instance, `app.log`)

 

5. Spring Boot Profiles in Logging

Spring Profiling is a good concept which provides us the flexibility to define properties for the different environment with no code change. Spring Boot provides the same profile mechanism. To use profile specific configuration files, we need to the naming convention of application-{profile}.properties where profile defines the name of the intended profile. Let’s take an example to create two separate logging configurations for Stage and Production environment.

 

5.1 application-stage.properties

logging.level.com.javadevjournal=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

 

5.2 application-production.properties

logging.level.com.javadevjournal=WARN
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

 

6. Log Group

it’s helpful to group related loggers together so it can configure them at the same time. Let’s take an example where we want to control the logging level for our application. Spring Boot allows you to define logging groups using property file:

logging.group.*= # Log groups to quickly change multiple loggers at the same time

Here is an example to for the reference:

logging.group.javadevjournal=com.javadevjournal.controller, com.javadevjournal.service, com.javadevjournal.util
#We are setting logging level for group using one line.
logging.level.javadevjournal=TRACE

For easy to use, Spring Boot provides following pre-defined groups:

  • web – org.springframework.core.codec, org.springframework.http, org.springframework.web
  • sql – org.springframework.core.codec, org.springframework.http, org.springframework.web

 

Summary

In this post, we saw how to enable Spring Boot logging with application.properties. We learned how to use the application.properties file to configure and customize the logging output for your application. The complete source code for this post is available on GitHub.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of