In this tutorial I will show you how to override 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.
application.properties

logging.level.org.springframework.web=ERROR
logging.level.com.roytuts.spring.boot=DEBUG

# Logging pattern for the console
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

# Logging pattern for file
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"

logging.file=SpringBoot.log

Modify the pom.xml file as shown below to use 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-log</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-log</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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 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 below Spring Boot main class

package com.roytuts.spring.boot.log.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootLogApplication implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootLogApplication.class);

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

    @Override
    public void run(String... args) throws Exception {
        LOGGER.info("Custom Logging configuration in SpringBoot 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 Eclipse console.

25-01-2017 17:46:29.900 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication - Starting SpringBootLogApplication on moni-pc with PID 316 (D:\mars_workspace\jcache\target\classes started by moni in D:\mars_workspace\jcache)
25-01-2017 17:46:29.903 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication - No active profile set, falling back to default profiles: default
25-01-2017 17:46:29.962 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b59d31: startup date [Wed Jan 25 17:46:29 IST 2017]; root of context hierarchy
25-01-2017 17:46:30.623 [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
25-01-2017 17:46:30.632 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication - Custom Logging configuration in SpringBoot application
25-01-2017 17:46:30.635 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication - Started SpringBootLogApplication in 0.972 seconds (JVM running for 1.439)
25-01-2017 17:46:30.636 [Thread-1] INFO  o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@b59d31: startup date [Wed Jan 25 17:46:29 IST 2017]; root of context hierarchy
25-01-2017 17:46:30.637 [Thread-1] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown

Console Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.0.RELEASE)

25-01-2017 17:46:29.900 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication -
                Starting SpringBootLogApplication on moni-pc with PID 316 (D:\mars_workspace\jcache\target\classes started by moni in D:\mars_workspace\jcache)
25-01-2017 17:46:29.903 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication -
                No active profile set, falling back to default profiles: default
25-01-2017 17:46:29.962 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext -
                Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b59d31: startup date [Wed Jan 25 17:46:29 IST 2017]; root of context hierarchy
25-01-2017 17:46:30.623 [main] INFO  o.s.j.e.a.AnnotationMBeanExporter -
                Registering beans for JMX exposure on startup
25-01-2017 17:46:30.632 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication -
                Custom Logging configuration in SpringBoot application
25-01-2017 17:46:30.635 [main] INFO  c.r.s.b.l.t.SpringBootLogApplication -
                Started SpringBootLogApplication in 0.972 seconds (JVM running for 1.439)
25-01-2017 17:46:30.636 [Thread-1] INFO  o.s.c.a.AnnotationConfigApplicationContext -
                Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@b59d31: startup date [Wed Jan 25 17:46:29 IST 2017]; root of context hierarchy
25-01-2017 17:46:30.637 [Thread-1] INFO  o.s.j.e.a.AnnotationMBeanExporter -
                Unregistering JMX-exposed beans on shutdown

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *