How to Change Log Level in Spring Boot

Spring boot has no mandatory dependency on any of the logging implementations. Spring boot only depends on commons logging API interface.  Commons logging API is a very thin bridge between logging clients and logging implementations. This enables application and framework writers to decouple application logging from actual logging implementations. Spring boot follows this approach and  uses the jcl-over-slf4j library for commons logging API interface. The enables backward compatibility to commons logging API but at the same time provides all the benefits of the new SLF4J API. This interface can be used to integrate any of the popular logging implementations such as Logback, Log4j2 or JDK logging.

If you are using any of the spring boot starter projects such as spring-boot-starter-web, it has a dependency on spring-boot-starter-logging. This starter library has a dependency on Logback logging library. The SLF4J API detects and configures Logback as the default logging implementation. Logback is considered to be successor to the popular log4j logging framework. Following are some of the benefits of Logback over log4J,

  • Logback is substantially faster than log4j and takes up less memory.
  • Logback has support for automatic reloading of configuration files.
  • With an exhaustive test suite, Logback is considered as a rock solid logging framework. This is very important since a logging framework is used across the application.
  • Logback-classic is a native implementation of the newer SLF4J API.
  • Built-in support for automatic removal of old logs and compression of the archival logs.
  • Built-in support for conditional processing of configuration files.

How to Change Log Level in Spring Boot Applications

The default out of the box logging configuration used by spring boot starter projects prints messages to the console. By default only ERROR, WARN and INFO messages are logged.

If you only want to change the log levels of various packages, you can configure it in the application.properties. For example, the following entries change the log level of all spring framework classes to ERROR and all the classes under the package com.quickprogrammingtips to DEBUG. We also sets the root logger to WARN.

# application.properties values
logging.level.root=WARN
logging.level.org.springframework=ERROR
logging.level.com.quickprogrammingtips=DEBUG

When you run with the above configuration, you will notice that very limited logging output is seen on the console.  This is because we have the root logger to WARN and spring framework classes to ERROR. Following is hierarchy of log levels in commons logging,

ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF

However for fine grained changes to Logback logging configuration, you can add a custom Logback configuration file (logback.xml) to the src/main/resources folder. It will be automatically picked up by spring boot. The following sample file shows how the logging levels can be set without using application.properties,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<include resource="org/springframework/boot/logging/logback/base.xml" />
	<logger name="org.springframework" level="ERROR" />
	<logger name="root" level="WARN" />
	<logger name="com.quickprogrammingtips" level="DEBUG" />
</configuration>

If you want to use spring boot templating features in logback configuration, create a file named logback-spring.xml instead of logback.xml. Much more complex customization is possible using logback-spring.xml. See spring boot documentation for more details.

How to Use Logging in Spring Boot Application Code

The preferred interface for calling log methods in spring boot is SLF4J API. However you can also use commons logging API. The following classes show how each API can be used for logging. Note that the actual logging implementation is selected based on what is available on the classpath. As mentioned earlier, if you are using starter projects, Logback is default implementation (other options are Log4j2 and JDK logging).

Using SLF4J API for Logging in Spring Boot Application Classes

package com.quickprogrammingtips.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {
    private static final Logger LOG = LoggerFactory.getLogger(HelloWorldController.class);

    @RequestMapping("/hello")
    @ResponseBody
    String home() {
        LOG.info("sending hello world response...");
        return "Hello World!";
    }
}

Using Commons Logging API in Spring Boot Application Classes

package com.quickprogrammingtips.springboot;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorld2Controller {
    private static final Log LOG = LogFactory.getLog(HelloWorldController.class);

    @RequestMapping("/hello2")
    @ResponseBody
    String home() {
        LOG.info("sending hello world 2 response...");
        return "Hello World!";
    }
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution! Please email us.

Leave a Reply