Logging Configuration in Spring Boot

Introduction

In this tutorial I will show you how logging configuration in Spring Boot applications by overriding Spring Boot’s default logging mechanism by creating logback.xml file under src/main/resources directory.

You may also read Simple log4j configuration in java and Log4j Configurations – Controlling Logging to Multiple Files

By default, slf4j logging is already included in the Spring Boot application, you just need to enable it.

To enable logging, create a application.properties file in the root of the src/main/resources folder.

Prerequisites

Spring Boot, Java

Build Configuration

The pom.xml file for maven build is shown below for the Spring Boot application.

<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>com.roytuts</groupId>
    <artifactId>spring-boot-logback</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
	
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
	
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.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>

If you are using gradle based project then use below configuration for build.gradle script.

buildscript {
	ext {
	   springBootVersion = '2.2.2.RELEASE'
    }
    repositories {
		mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}")
}

Create logback.xml

If you want to override the Spring Boot’s default logging template, you need to just create a standard logback.xml in the src/main/resources folder. This will override the Spring Boot’s default logging template.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CA" class="ch.qos.logback.core.ConsoleAppender">
        <!-- Log message format -->
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
                %msg%n
            </pattern>
        </encoder>
    </appender>
	
    <appender name="RFA" class="ch.qos.logback.core.FileAppender">
        <file>SpringBoot.log</file>
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
	
    <root>
        <level value="INFO" />
        <appender-ref ref="CA" />
        <appender-ref ref="RFA" />
    </root>
</configuration>

Create Main Class

Create below Spring Boot main class to create some log statements in the console as well as in the file.

package com.roytuts.spring.boot.logback;

import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootLogbackApp implements CommandLineRunner {

	private static final Logger LOG = LoggerFactory.getLogger(SpringBootLogbackApp.class);

	public static void main(String[] args) {
		SpringApplication.run(SpringBootLogbackApp.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		printLog();
	}

	private static void printLog() {
		LOG.debug("Debug Message");
		LOG.warn("Warn Message");
		LOG.error("Error Message");
		LOG.info("Info Message");
		LOG.trace("Trace Message");
	}

}

Testing the Application

Now when you run the above class then you should see the log file  SpringBoot.log with below content created under the application root directory. At the same time you will also see the logging in the console.

Source Code

Download

Thanks for reading.

Leave a Comment