Spring Boot Logging Example
December 03, 2023
1. Spring Boot can use Logback, Log4J2 and Java util logging.
2. By default, Spring Boot uses Logback and logs in console. We can change the configuration to log in files.
3. The JAR dependency for Logback is resolved by spring-boot-starter-logging starter.
4. When we use any Spring Boot starter, spring-boot-starter-logging is resolved by default. We need not to include it separately.
5. If Logback JAR is available in classpath, Spring Boot always chooses Logback for logging. To use other logging such as Log4J2, we need to exclude Logback JAR and add Log4J2 JAR in classpath.
6. To use Logback, we have to do nothing, just configure logging level in
application.properties
or application.yml
and we are done.
7. By default,
ERROR
, WARN
and INFO
log level messages are logged in console. To change log level, use logging.level property.
8. To get logs in file, we need to configure logging.file or logging.path in property file. Log files rotates when they reach 10 MB.
9.Find the properties to configure for logging in property file.
logging.level.* : Used as prefix with package name to set log level.
logging.file : Configures a log file name to log message in file. We can also configure file name with absolute path.
logging.path : Only configures path for log file. Spring boot creates a log file with name
spring.log
.
logging.pattern.console : Defines logging pattern in console.
logging.pattern.file: Defines logging pattern in file.
logging.pattern.level: Defines the format to render log level. Default is
%5p
.
logging.exception-conversion-word : Defines conversion word when logging exceptions.
PID : Defines the current process ID.
We will discuss here how to use Logback and configure log properties using
application.properties
and application.yml
and Logback XML file. We will also provide how to enable Log4J2 logging in our spring boot application.
Contents
1. logging.level
logging.level is used to set log level such asTRACE
, DEBUG
, INFO
, WARN
, ERROR
, FATAL
, OFF
. Logging level can be set at root level as well as package level. Find the example.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
1. Using
src\main\resources\application.properties
logging.level.root= WARN logging.level.org.springframework.security= DEBUG logging.level.org.springframework.web= ERROR logging.level.org.hibernate= DEBUG logging.level.org.apache.commons.dbcp2= DEBUG
src\main\resources\application.yml
logging: level: root: WARN org: springframework: security: DEBUG web: ERROR hibernate: DEBUG apache: commons: dbcp2: DEBUG
2. Using logging.file
By default, Spring Boot displays logs on console. To log it on log file, we need to define either logging.file or logging.path property. logging.file property is used to define log file name. It can be only file name or file name with path.Example:
logging.file = mylogfile.log
mylogfile.log
is created in the root directory of the project. We can also assign a path to log file as
concretepage/mylogfile.log
. In this case log file will be created with path concretepage/mylogfile.log
relative to root directory of the project. We can also assign an absolute path for log file location.
file: mylogfile.log
3. Using logging.path
Here we will learn to use logging.path property. This property is used to define path for log file. A log file with namespring.log
will be created in the given path.
Example:
logging.path = concretepage/logs
spring.log
will be created with path concretepage/logs/spring.log
relative to root directory of the project. We can also assign an absolute path for log file location.
Using
application.yml
path: concretepage/logs
4. logging.pattern.console
To change logging pattern in console, we need to use logging.pattern.console property. The logging pattern includes date, time, log level, thread name, logger name and message.Using
application.properties
logging.pattern.console= %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n
application.yml
pattern: console: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
5. logging.pattern.file
To change log pattern in file, we need to configure logging.pattern.file in property file. First of all we need to enable logging in file. Logging in file can be enabled by configuring either logging.file or logging.path in property file.Using
application.properties
logging.path = concretepage/logs logging.pattern.file= %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n logging.pattern.console= %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n
spring.log
will be created in concretepage/logs relative to root directory of the project. The property logging.pattern.file will set log pattern in file and logging.pattern.console will set log pattern in console.
Using
application.yml
path: concretepage/logs pattern: file: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n' console: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
6. Change Log Level in Console Output
ERROR
, WARN
and INFO
log levels are displayed by default. We can enable DEBUG
and TRACE
log levels using command line as well as property file.
Suppose we have an executable jar named as my-app.jar, to enable
DEBUG
level, start the application as following.
java -jar my-app.jar --debug
Using
application.properties
debug=true
application.yml
debug: true
TRACE
level logging using command line.
java -jar my-app.jar --trace
Using
application.properties
trace=true
application.yml
trace: true
7. Using Logging in Our Application
We are creating an example that will use SLF4J with Logback. To use logging in our classes, we need to instantiateorg.slf4j.Logger
in our class. Find the example.
MyApplication.java
@SpringBootApplication public class MyApplication { private static final Logger logger = LoggerFactory.getLogger(MyApplication.class); public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); logger.debug("--Application Started--"); } }
com.concretepage
with other logging configuration in application.properties
.
logging.level.root= WARN logging.level.org.springframework.web= ERROR logging.level.com.concretepage= DEBUG
2017-03-25 19:03:54.189 DEBUG 4828 --- [ main] com.concretepage.MyApplication : Running with Spring Boot v1.5.2.RELEASE, Spring v4.3.7.RELEASE 2017-03-25 19:03:54.189 INFO 4828 --- [ main] com.concretepage.MyApplication : No active profile set, falling back to default profiles: default 2017-03-25 19:03:58.846 INFO 4828 --- [ main] com.concretepage.MyApplication : Started MyApplication in 5.209 seconds (JVM running for 5.66) 2017-03-25 19:03:58.846 DEBUG 4828 --- [ main] com.concretepage.MyApplication : --Application Started--
8. Using Logback XML file
To use XML configuration for Logback, we need to createlogback-spring.xml
file in classpath.
src\main\resources\logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="ERROR"/> <logger name="com.concretepage" level="DEBUG"/> </configuration>
9. Using Log4j2
When we use any Spring Boot starter, Logback dependency spring-boot-starter-logging is resolved in classpath by default. To use Log4j2, we need to exclude it and include spring-boot-starter-log4j2 as following.<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
log4j2-spring.xml
in classpath and configure logging.
10. References
Spring Boot features: LoggingHow-to guides: Logging