Logging is a very import aspect when developing spring boot applications. It makes easier to debug the issues that occurs during application’s runtime.

It is also very useful to log important transactions that occurs in a application into a log file for future reference.

spring boot logging example

Spring boot provides logging support by adding spring-boot-starter-logging transitive dependency by default in every spring-boot-starter dependency modules.

Spring boot supports multiple logging libraries including Log4J, Logback, SLF4J, etc.

Spring boot auto configures logging, based on the log configuration files available in the spring boot application’s class path.

In this article, we will learn how to use logging while creating spring boot applications.

Few basic logging properties are given below.

#Logging properties:
logging.level.org.springframework.web=debug
logging.level.org.hibernate=debug

In above spring boot logging configuration, we are setting hibernate, spring related logs to debug level.

Few of the available configuration properties are given below :
  • logging.path : This property can be used to set logging file path(Ex: D:/logs/).
  • logging.file : To set the logging file name. We can use either this property with complete file path(Ex: D:/logs/my_app.log) or use above property, but not both of them together.
  • logging.pattern.dateformat : This property can be used to logging date and time format used while writing spring boot application logs. Example pattern value: dd-MM-yyyy HH:mm:ss.SSS
  • logging.config : This property can be used to set the location of the logging configuration file. Default value is classpath:logback.xml
Using Logback for logging

Spring boot applications supports Logback logging by default. To enable Logback logging in our spring boot application, we have to place the logback configuration under application’s class path.

Name of the Logback logging configuration can be logback.xml or logback-spring.xml.

One of the main advantage of using Logback logging in spring boot application is no additional configuration need to be added under application.properties file.

An example Logback configuration file is given below.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<springProfile name="default">
		<appender name="CONSOLE"
			class="ch.qos.logback.core.ConsoleAppender">
			<encoder>
				<pattern>%msg%n</pattern>
			</encoder>
		</appender>

		<appender name="FILE"
			class="ch.qos.logback.core.FileAppender">
			<file>myApp_default.log</file>
			<encoder>
				<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
			</encoder>
		</appender>

		<root level="DEBUG">
			<appender-ref ref="CONSOLE" />
		</root>
		<root level="DEBUG">
			<appender-ref ref="FILE" />
		</root>
	</springProfile>
	<springProfile name="PROD">
		<appender name="CONSOLE"
			class="ch.qos.logback.core.ConsoleAppender">
			<encoder>
				<pattern>%msg%n</pattern>
			</encoder>
		</appender>

		<appender name="FILE"
			class="ch.qos.logback.core.FileAppender">
			<file>myApp_prod.log</file>
			<encoder>
				<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
			</encoder>
		</appender>

		<root level="DEBUG">
			<appender-ref ref="CONSOLE" />
		</root>
		<root level="INFO">
			<appender-ref ref="FILE" />
		</root>
	</springProfile>
</configuration>

In above Logback spring boot logging configuration file, we have created two different configurations for spring boot profiles : default and PROD.

More logging configuration related to Logback logging is available here.

Log4J Example in spring boot application

To use Log4J or Log4J2 logging with spring boot application, we have to exclude spring-boot-starter-logging dependency as shown below.

<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>

Next, we can include required starter dependency as given below.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

Now we can use log4j.properties or log4j.xml configuration files to configure logging of spring boot application.

Logging configuration file should be placed under spring boot application class path.

Sample log4j.properties configuration file is given below. Here we have used both Console appender and File appenders for logging.

log4j.rootLogger=DEBUG,fout
log4j.logger.com.endeca=INFO
log4j.logger.com.endeca.itl.web.metrics=INFO

#Console uppender properties:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

#File uppender properties:
log4j.appender.fout=org.apache.log4j.FileAppender
log4j.appender.fout.File=myapp_log4j.log
log4j.appender.fout.layout=org.apache.log4j.PatternLayout
log4j.appender.fout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

Conclusion

Logging is very important part of any application, which may help in debugging errors, logging important information, etc.

Spring boot supports multiple logging libraries as seen above. For most of the cases Logback is sufficient as it has most of the features supported by other logging libraries.

Source code is available here. Happy coding 🙂

You may also interested in :