Logging is an important part of any application. Checking the logs is the first step towards debugging any issue. So logging the right information is important. At the same time logging too much can lead to bloated log file in turn using lot of disk space. In some applications developers use synchronous logging which can impact the application’s performance. Logging libraries like Logback, Log4j2 provide async logging.
- Also Read : Spring Boot Tutorials
Spring Boot provides great support for logging and provides lot of hooks to configure the same. In this article we are going to see the default support for logging in Spring Boot, then use the hooks i.e the spring properties to configure the logging. At the end of this article, you will be familiar with the logging configuration in spring boot applications.
Create Spring Boot Project using Maven
Let us first create a simple maven project and update the pom.xml
with the below code:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.javabeat</groupId> <artifactId>springboot-logging</artifactId> <version>0.1</version> <name>SpringBoot Logging</name> <properties> <java.version>1.8</java.version> <start-class>net.javabeat.SpringBootLoggingDemo</start-class> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The dependency spring-boot-starter
includes the dependencies for logging spring-boot-starter-logging
, so we need not add the logging dependency again. The spring-boot-starter-logging
includes SLF4J and logback dependencies with appropriate SLF4J wrappers for other logging libraries.
Default Logging Support in Spring Boot
Spring Boot reference document says:
By default, If you use the ‘Starter POMs’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.
Let us create SpringBootLoggingDemo
class in net.javabeat
package which will be the starting point for our application. By default the logging will be written to the console using a fixed logging format used by logback as shown below:
09:59:03.086 [net.javabeat.SpringBootLoggingDemo.main()] ERROR net.javabeat.SpringBootLoggingDemo - Message logged at ERROR level
The above log contains of following parts:
- Time with millisecond precision
- Name of the thread in square brackets ([])
- Log level
- Logger name, generally class name truncated for brevity
- Actual log message
The default log level configured by logback is DEBUG
i.e any messages logged at ERROR
, WARN
, INFO
and DEBUG
get printed on the console. Let us create the SpringBootLoggingDemo
class with a logger and log messages at ERROR, WARN, INFO and DEBUG level as shown below:
package net.javabeat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootLoggingDemo { private static final Logger logger = LoggerFactory.getLogger(SpringBootLoggingDemo.class); public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(new Object[]{SpringBootLoggingDemo.class}); springApplication.run(args); logger.error("Message logged at ERROR level"); logger.warn("Message logged at WARN level"); logger.info("Message logged at INFO level"); logger.debug("Message logged at DEBUG level"); } }
Run the application using the command: mvn spring-boot:run
to see the log messages printed on the console as shown:
10:01:40.996 [net.javabeat.SpringBootLoggingDemo.main()] ERROR net.javabeat.SpringBootLoggingDemo - Message logged at ERROR level 10:01:40.996 [net.javabeat.SpringBootLoggingDemo.main()] WARN net.javabeat.SpringBootLoggingDemo - Message logged at WARN level 10:01:40.996 [net.javabeat.SpringBootLoggingDemo.main()] INFO net.javabeat.SpringBootLoggingDemo - Message logged at INFO level 10:01:40.996 [net.javabeat.SpringBootLoggingDemo.main()] DEBUG net.javabeat.SpringBootLoggingDemo - Message logged at DEBUG level
Modifying the default configuration by providing logback.xml
Let us now modify the default logback configuration by providing a logback.xml configuration file as shown below:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- Log message format --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <!-- Setting the root level of logging to INFO --> <root level="info"> <appender-ref ref="STDOUT" /> </root> </configuration>
The above configuration does the following:
- Set up a appender STDOUT using ConsoleAppender which prints to the console
- Provide a pattern to the appender to build the log message
- Set up a root logger which logs any message above INFO level using the STDOUT appender
We have to place this logback.xml in the classpath of the application for Spring Boot to pick the configuration. I have placed this in src/main/resources as shown below:
Let us run the application again using
mvn spring-boot:run
and see that only ERROR, WARN, INFO
are logged and the one DEBUG
is not logged as shown below:
14:04:23.805 [net.javabeat.SpringBootLoggingDemo.main()] ERROR net.javabeat.SpringBootLoggingDemo - Message logged at ERROR level 14:04:23.808 [net.javabeat.SpringBootLoggingDemo.main()] WARN net.javabeat.SpringBootLoggingDemo - Message logged at WARN level 14:04:23.809 [net.javabeat.SpringBootLoggingDemo.main()] INFO net.javabeat.SpringBootLoggingDemo - Message logged at INFO level
This was a simple logback.xml configuration. Let us see how we can configure it to set different log levels for different java packages in the application. For that let us first create two classes: TestModel.java
in net.javabeat.model
and TestService.java
in net.javabeat.service
as as shown below:
TestModel.java
package net.javabeat.model; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestModel { private static final Logger logger = LoggerFactory.getLogger(TestModel.class); public TestModel(){ logger.debug("Log message at DEBUG level from TestModel constructor"); logger.info("Log message at INFO level from TestModel constructor"); } }
TestService.java
package net.javabeat.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestService { private Logger logger = LoggerFactory.getLogger(TestService.class); public void service(){ logger.info("Message at INFO level from TestService.service()"); logger.warn("Message at WARN level from TestService.service()"); } }
The project structure with these new files looks like below:
We are going to set the log level to WARN for net.javabeat.service package and to INFO for net.javabeat.model package as show below:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- Log message format --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Setting the logging level to WARN for code in net.javabeat.service --> <logger name="net.javabeat.service" level="WARN" /> <!-- Setting the logging level to INFO for code in net.javabeat.model --> <logger name="net.javabeat.model" level="INFO" /> <!-- Setting the root level of logging to INFO --> <root level="info"> <appender-ref ref="STDOUT" /> </root> </configuration>
We would have to update the SpringBootLoggingDemo
class and add the below lines just before creating a new instance of SpringApplication
:
TestModel model = new TestModel();
TestService service = new TestService();
service.service();
Now, running the application give us the below output:
17:36:07.616 [net.javabeat.SpringBootLoggingDemo.main()] ERROR net.javabeat.SpringBootLoggingDemo - Message logged at ERROR level 17:36:07.632 [net.javabeat.SpringBootLoggingDemo.main()] WARN net.javabeat.SpringBootLoggingDemo - Message logged at WARN level 17:36:07.632 [net.javabeat.SpringBootLoggingDemo.main()] INFO net.javabeat.SpringBootLoggingDemo - Message logged at INFO level 17:36:07.632 [net.javabeat.SpringBootLoggingDemo.main()] INFO net.javabeat.model.TestModel - Log message at INFO level from TestModel constructor 17:36:07.632 [net.javabeat.SpringBootLoggingDemo.main()] WARN net.javabeat.service.TestService - Message at WARN level from TestService.service()
You can observe that for TestModel the message at DEBUG level was not logged and for TestService the message at INFO level was not logged.
Sending the log messages to a file
In this tutorial I am using the logback.xml
as the logging configuration. Spring boot by default uses the logback as the default logging implementation if you are not specifying any other implementations. If you want to write the log files to an file but you don’t want to use the logback.xml
file for the configurations, you can use the spring boot properties logging.path
or logging.file
to specify the location of the log files.
Spring boot will look for the above two properties, if those are are not specified then the log messages are sent to the console appender. Otherwise the log messages will be sent to file specified by those properties.

Now let’s look at how to use the logback for printing the log message sin the files. Till now we saw the log messages being printed on console, let us now see how the same can be written to a file. We need to update logback.xml configuration to add a new appender to write to file and then use that appender in the loggers defined in the configuration file as shown below:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- Log message format --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Ned appender to write to file --> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <!-- Name of the file where the log messages are written --> <file>myApp.log</file> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="net.javabeat.service" level="WARN"> <appender-ref ref="FILE" /> </logger> <logger name="net.javabeat.model" level="INFO"> <appender-ref ref="FILE" /> </logger> <!-- Setting the root level of logging to INFO --> <root level="info"> <appender-ref ref="FILE" /> </root> </configuration>
Now running the application will redirect all the log messages to the file myApp.log
present in the current directory.
Making use of default logback configuration base.xml
Till now we haven’t made use of Spring Boot features or the defaults it provides. In this section we are going to show you how we can make use of the logback configuration base.xml. Base.xml sets up the following things:
- Wiring the spring properties- logging.file, logging.path with the logback configuration
- Setting up console and file appenders
- Setting log levels for some packages
Let us update the logback.xml configuration file with the below configuration:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="net.javabeat.service" level="WARN"></logger> <logger name="net.javabeat.model" level="INFO"></logger> </configuration>
And add application.properties
file with a property logging.file
having the name of the file as shown: logging.file=demo_logging.log
. The logging will now be written both to the console and to the file. Let us verify this by running the application using mvn spring-boot:run
to get the below log messages printed on console and in the file demo_logging.log:
... 2015-08-18 19:35:09.725 ERROR 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at ERROR level 2015-08-18 19:35:09.725 WARN 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at WARN level 2015-08-18 19:35:09.725 INFO 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at INFO level 2015-08-18 19:35:09.726 INFO 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.model.TestModel : Log message at INFO level from TestModel constructor 2015-08-18 19:35:09.727 WARN 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.service.TestService : Message at WARN level from TestService.service() ... C:\Users\Mohamed\workspace\springboot-logging>more demo_logging.log ... 2015-08-18 19:35:09.725 ERROR 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at ERROR level 2015-08-18 19:35:09.725 WARN 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at WARN level 2015-08-18 19:35:09.725 INFO 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.SpringBootLoggingDemo : Message logged at INFO level 2015-08-18 19:35:09.726 INFO 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.model.TestModel : Log message at INFO level from TestModel constructor 2015-08-18 19:35:09.727 WARN 7316 --- [net.javabeat.SpringBootLoggingDemo.main()] net.javabeat.service.TestService : Message at WARN level from TestService.service()
You can see that with a minimal logback.xml we have achieved a lot more, thanks to the defaults provided by Springboot. The code for this article can be found here.
Conclusion
I hope this tutorial have provided enough insight on how to use the logging mechanism in Spring Boot application. Also I have written about the default logging support in the Spring Boot applications and how to override them by modifying the logback.xml file. If you have any questions in logging configuration in spring boot, please write it in the comments section.