Spring Boot Logging

Spring boot makes log configuration easy as it provides default configuration for logging and allows you to use JDK Logging, Log4J2 or Logback log implementations. By default spring boot uses Logback.

In this tutorial, let’s see how logging works without configuration and how to add log statements, customize default log configuration and use different log implementations in spring boot applications.

Project Setup

First create spring boot project and add starters, please see spring boot example and spring starters tutorials for reference. For testing logging, lets create web application, so we need to add spring-boot-starter-web starter to pom and update the project.

If you use any spring boot starter in your project, jars related log implementations such as log4j2 and logback jars are included and added to the classpath.

spring boot logging dependencies

Run the Application

Let’s create a controller with spring and spring boot annotations, add main method and run it. You will see logs printed on console and each log message contains date and time, log level, process id, separator, thread name, class name and log message.

Since spring frameworks use common logging interfaces for log statements, log output works with any log implementation.

spring boot logging default logs

Adding Log Statements

Now let’s add log statements in our controller. To do that, we need to get logger object by calling getLagger() on LoggerFactory and using logger object you can add log statements by calling debug, info, warn, error, etc.. methods on it.

public class App {
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	public static void main(String[] args) {, args);


	String checkLogs() {"Spring Boot log configuration tutorial");
		return "Log Configuration Spring Boot";

Then run the application, access the app from browser using http://localhost:8080 url and notice the log message printed on the console.

spring boot log statements

Log File

We have seen, by default, spring boot outputs log messages to console. To get log messages written to a file also, we need to pass log file name to Spring Boot by adding logging.file property in file which needs to be saved under src/main/resources folder.

spring boot writing logs to file

Setting Log Level

To pass log level setting to Spring Boot, we need to add log level setting in file.


Making Spring Boot Use Different Logging System

So far we have seen the default logging system Logback being used by Spring Boot. If you want to use different log implementation, you can let Spring Boot know by passing the log system as system property.

LoggingSystem is an interface defined in spring boot. For each log system, there is an implementation of the interface in spring boot, for jdk logging it is JavaLoggingSystem, for log4j2 it is Log4J2LoggingSystem, for log4j it is Log4JLoggingSystem and for logback it is LogbackLoggingSystem. You need to pass the implementation class name as value for LoggingSystem property to spring boot.

For example, if you want to force spring boot to use jdk logging, here is the option you need to pass when you start the application.
spring boot writing logs to file

Here is the jdk logs on console.

spring boot jdk logging

Making Spring Boot Use Log4j2 Logging

Like shown above, Log4JLoggingSystem class needs to be passed as value for LoggingSystem property to force spring boot use log4j2 logging, but some missing log4j and sl4j jars dependencies have to be added in order for it to work. Instead of that, we can just exclude spring-boot-starter-logging starter and add spring-boot-starter-log4j2 starter to pom to make the spring boot use log4j2.


Spring Boot Custom Log Configuration

Logs can be customized by defining log configuration for the log system being used and adding it to the classpath or main/resources directory. Spring boot will pick the configuration and pass it to the log system.

If you are using log4j2 log system, you need define log configuration in log4j2-spring.xml or log4j2.xml. The log configuration file name for Logback can be either logback-spring.xml or logback.xml and the file name for jdk logging is

Here is the custom log configuration for log4j2.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
		<Console name="Console" target="SYSTEM_OUT" follow="true">
			<PatternLayout pattern="%d{MM-dd HH:mm:ss.SSS} %5p %logger{36} : %m%n" />
		<RollingFile name="File" fileName="${sys:LOG_FILE}" filePattern="${sys:LOG_PATH}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
				<Pattern>%d{MM-dd HH:mm:ss.SSS} ${sys:PID} %5p %C{1.} : %m%n</Pattern>
				<SizeBasedTriggeringPolicy size="5 MB" />
		<logger name="com.grabobj" level="warn"/>
		<Root level="info">
			<AppenderRef ref="Console" />
			<AppenderRef ref="File" />

Here is the output of the logs with the custom log configuration file defined above.

spring boot logging custom configuration log output