Spring Boot Logging

Writing the Code

You can use various logging frameworks, including Commons Logging, Log4j, SLF4J, and Logback.

Main.java

package sample.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@EnableConfigurationProperties
public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        try (ConfigurableApplicationContext ctx = SpringApplication.run(Main.class, args)) {
            logger.error("error log");
            logger.warn("warn log");
            logger.info("info log");
            logger.debug("debug log");
            logger.trace("trace log");
        }
    }
}

Result

2015-04-29 17:31:25.023 ERROR 8872 --- [           main] sample.springboot.Main                   : error log
2015-04-29 17:31:25.023  WARN 8872 --- [           main] sample.springboot.Main                   : warn log
2015-04-29 17:31:25.023  INFO 8872 --- [           main] sample.springboot.Main                   : info log
  • By default, logs at INFO level and above are printed.
  • The format is date, log level, PID, thread name, logger name, and log message.

File Output

By default, logs are written only to standard output. Specify a log file to write them to a file as well. Log files rotate every 10 MB.

Specifying a File Name

$ java -jar build/libs/spring-boot-sample.jar --logging.file=sample.log

$ dir /b *.log
sample.log
  • Specify the output file name with logging.file.
  • The example uses a command-line argument, but you can also configure it in a properties file.
  • The target directory is created automatically if it does not exist.

Specifying a Folder

$ java -jar build/libs/spring-boot-sample.jar --logging.path=logs

$ dir /b logs
spring.log
  • Specify the log output folder with logging.path.
  • The log file name becomes spring.log.
  • The directory is created automatically if it does not exist.

Configuring the Log Level for Each Logger

$ java -jar build/libs/spring-boot-sample.jar --logging.level.sample.springboot.Main=TRACE

2015-04-29 18:14:17.969 ERROR 8288 --- [           main] sample.springboot.Main                   : error log
2015-04-29 18:14:17.970  WARN 8288 --- [           main] sample.springboot.Main                   : warn log
2015-04-29 18:14:17.970  INFO 8288 --- [           main] sample.springboot.Main                   : info log
2015-04-29 18:14:17.970 DEBUG 8288 --- [           main] sample.springboot.Main                   : debug log
2015-04-29 18:14:17.970 TRACE 8288 --- [           main] sample.springboot.Main                   : trace log
  • Configure a logger level with logging.level.[logger]=[log level].
  • If logger names use FQCNs, you can configure them by package, such as --logging.level.sample.springboot=DEBUG.