Logging Configuration in Spring Boot


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.


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

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

buildscript {
	ext {
	   springBootVersion = '2.2.2.RELEASE'
    repositories {
    dependencies {

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

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

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"?>
    <appender name="CA" class="ch.qos.logback.core.ConsoleAppender">
        <!-- Log message format -->
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
    <appender name="RFA" class="ch.qos.logback.core.FileAppender">
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        <level value="INFO" />
        <appender-ref ref="CA" />
        <appender-ref ref="RFA" />

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;

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

	public void run(String... args) throws Exception {

	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


Thanks for reading.

Related posts

Leave a Comment