Spring Boot Logging Example

By Arvind Rai, March 26, 2017
On this page we will provide spring boot logging example. Spring boot can use Logback, Log4J2, java util logging. By default spring boot uses Logback for its logging. By default log is logged in console and can also be logged in files. The JAR dependency for Logback is resolved by spring-boot-starter-logging. When we use any spring boot starter then spring-boot-starter-logging is resolved by default. We need not to include it separately. If Logback JAR is available in classpath then spring boot will always choose Logback for logging. So to use other logging such as Log4J2, we need to exclude Logback JAR and add Log4J2 JAR in classpath. To use Logback logging we have to do nothing, just configure logging level in application.properties or application.yml and we are done. By default ERROR, WARN and INFO log level messages are logged in console. To change log level, use logging.level property. To get logs in file, we can configure logging.file or logging.path in property file. Log files will rotate when they reach 10 MB. Find the spring environment that is configured in property file.

logging.level.* : It is used as prefix with package name to set log level.
logging.file : It configures a log file name to log message in file. We can also configure file name with absolute path.
logging.path : It only configures path for log file. Spring boot creates a log file with name spring.log.
logging.pattern.console : It defines logging pattern in console.
logging.pattern.file: It defines logging pattern in file.
logging.pattern.level: It defines the format to render log level. Default is %5p.
logging.exception-conversion-word : It defines conversion word when logging exceptions.
PID : It 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.

logging.level

logging.level is used to set log level. Logging level can be one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Logging level can be set at root level as well as package level. For the demo we are creating an application that has following dependency.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>  
We can set logging level as follows. 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  
Find the print screen for the location of property file.
Spring Boot Logging Example
Now using src\main\resources\application.yml
logging:
  level:
    root: WARN        
    org:
      springframework:
        security: DEBUG
        web: ERROR    
      hibernate: DEBUG        
      apache:
        commons:
          dbcp2: DEBUG  

File Output using logging.file

By default spring boot displays log on console but if we want to log it on log file then we need to define either logging.file or logging.path property. Here we will understand the use of logging.file property. logging.file property is used to define log file name. It can be only file name or file name with path.

Example to configure logging.file in application.properties.
logging.level.org.springframework.security= DEBUG
logging.level.org.hibernate= DEBUG

logging.file = mylogfile.log  
In this case, a log file with name mylogfile.log will be 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.

Example to configure logging.file in application.yml.
logging:
  level:
    org:
      springframework:
        security: DEBUG
    hibernate: DEBUG

  file: mylogfile.log  

File Output using logging.path

To get log in file, we need to define either logging.file or logging.path property. Here we will understand the use of logging.path property. logging.path property is used to define path for log file. A log file with name spring.log will be created in the given path.

Example to configure logging.path in application.properties.
logging.level.org.springframework.security= DEBUG
logging.level.org.hibernate= DEBUG

logging.path = concretepage/logs  
A log file named as 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.

Example to configure logging.path in application.yml.
logging:
  level:
    org:
      springframework:
        security: DEBUG
    hibernate: DEBUG

  path: concretepage/logs  

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. We can change log pattern in console according our requirement as follows.
Using application.properties
logging.level.org.springframework.security= DEBUG
logging.level.org.hibernate= DEBUG

logging.pattern.console= %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n  
Using application.yml
logging:
  level:
    org:
      springframework:
        security: DEBUG
    hibernate: DEBUG

  pattern:
    console: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'  

logging.pattern.file

To change log pattern in file we need to configure logging.pattern.file in property file. But 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.level.org.springframework.security= DEBUG
logging.level.org.hibernate= DEBUG

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  
To enable logging in file we are using logging.path property. A log file named as 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
logging:
  level:
    org:
      springframework:
        security: DEBUG
    hibernate: DEBUG
    
  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'  

Change Log Level in Console Output using Command Line

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, then to enable DEBUG level, start the application as follows.
java -jar my-app.jar --debug  
The same can also be achieved by property file by configuring property as follows.
Using application.properties
debug=true  
Using application.yml
debug: true  
In the same way we can enable TRACE level logging using command line.
java -jar my-app.jar --trace  
The same can also be achieved by property file by configuring property as follows.
Using application.properties
trace=true  
Using application.yml
trace: true  

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 instantiate org.slf4j.Logger in our class. Find the example.
MyApplication.java
package com.concretepage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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--");
        }       
}  
Enable logging for the package com.concretepage with other logging configuration in application.properties.
logging.level.root= WARN
logging.level.org.springframework.web= ERROR
logging.level.com.concretepage= DEBUG  
Find the output.
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--  

Using Logback XML file

By default spring boot uses Logback and we configure log level in application.properties or application.yml. If we want to use XML configuration for Logback then we need to create logback-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>  

Using Custom Log: 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 follows.
<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>  
Now create log4j2-spring.xml in classpath and configure logging.

Now I am done. Happy Spring Boot Learning!

References

Spring Boot features: Logging
How-to guides: Logging
POSTED BY
ARVIND RAI
ARVIND RAI









©2023 concretepage.com | Privacy Policy | Contact Us