Spring Boot - Logging
Spring Boot - Logging
Spring Boot - Logging
Spring Boot uses Apache Commons logging for all internal logging. Spring Boot’s default
configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. Using
these, we can configure the console logging as well as file logging.
If you are using Spring Boot Starters, Logback will provide a good support for logging. Besides,
Logback also provides a use of good support for Common Logging, Util Logging, Log4J, and
SLF4J.
Log Format
The default Spring Boot Log format is shown in the screenshot given below.
Process ID
If you have to enable the debug level log, add the debug flag on starting your application using the
command shown below −
You can also add the debug mode to your application.properties file as shown here −
debug = true
https://www.tutorialspoint.com/spring_boot/spring_boot_logging.htm 1/4
7/31/22, 7:46 PM Spring Boot - Logging
You can specify the log file path using the property shown below. Note that the log file name is
spring.log.
logging.path = /var/tmp/
You can specify the own log file name using the property shown below −
logging.file = /var/tmp/mylog.log
Note − files will rotate automatically after reaching the size 10 MB.
Log Levels
Spring Boot supports all logger levels such as “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”,
“FATAL”, “OFF”. You can define Root logger in the application.properties file as shown below −
logging.level.root = WARN
Note − Logback does not support “FATAL” level log. It is mapped to the “ERROR” level log.
Configure Logback
Logback supports XML based configuration to handle Spring Boot Log configurations. Logging
configuration details are configured in logback.xml file. The logback.xml file should be placed
under the classpath.
You can configure the ROOT level log in Logback.xml file using the code given below −
<configuration>
</root>
</configuration>
You can configure the console appender in Logback.xml file given below.
<configuration>
</root>
</configuration>
https://www.tutorialspoint.com/spring_boot/spring_boot_logging.htm 2/4
7/31/22, 7:46 PM Spring Boot - Logging
You can configure the file appender in Logback.xml file using the code given below. Note that you
need to specify the Log file path insider the file appender.
<configuration>
<File>/var/tmp/mylog.log</File>
</appender>
</root>
</configuration>
You can define the Log pattern in logback.xml file using the code given below. You can also define
the set of supported log patterns inside the console or file log appender using the code given below
−
The code for complete logback.xml file is given below. You have to place this in the class path.
<configuration>
<encoder>
</encoder>
</appender>
<File>/var/tmp/mylog.log</File>
<encoder>
</encoder>
</appender>
</root>
</configuration>
The code given below shows how to add the slf4j logger in Spring Boot main class file.
package com.tutorialspoint.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
https://www.tutorialspoint.com/spring_boot/spring_boot_logging.htm 3/4
7/31/22, 7:46 PM Spring Boot - Logging
SpringApplication.run(DemoApplication.class, args);
The output that you can see in the console window is shown here −
The output that you can see in the log file is shown here −
https://www.tutorialspoint.com/spring_boot/spring_boot_logging.htm 4/4