Logback Tutorial for Beginners (original) (raw)

In this post, we feature a comprehensive Logback Tutorial. Logback is one of the most widely used logging frameworks in the Java community. It provides more options for configuration and more flexibility in archiving old log files. In this tutorial, I will demonstrate its three key components: Logger, Appender, and Layout.

1. Introduction

Most of Java applications rely on the logging messages to identify and troubleshoot problems. Logback is one of the most widely used logging frameworks in the Java community. It offers a faster implementation than Log4j, provides more options for configuration, and more flexibility in archiving old log files.

The Logback architecture includes three classes:

Most Java applications log the messages via objects –Logger and configure Appenders with the desired layout pattern in a configuration file. In this example, I will build a Spring boot application to demonstrate the Logback’s fundamental functions:

2. Technologies Used

The example code in this article was built and run using:

3. Spring Boot Application

There are many ways to create a Spring boot application. The easiest way for me is via the Spring starter tool with the steps below:

  1. Go to:https://start.spring.io/
  2. Select Maven Project, Java, and Spring Boot version 2.0.3
  3. Enter the group name as jcg.zheng.demo and the artifact name as logback-demo
  4. ClickGenerate Project

A maven project will be generated and downloaded to your workstation. Import it into your Eclipse workspace. There is no modification needed for the two generated files in this example.

3.1 Dependency

Pom.xml manages the project libraries.

pom.xml

4.0.0

<groupId>jcg.zheng.demo</groupId>
<artifactId>logback-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>logback-demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

3.2 LogbackDemoApplication

LogbackDemoApplication.java starts the application.

LogbackDemoApplication.java

package jcg.zheng.demo.logbackdemo;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class LogbackDemoApplication {

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

}

4. Log a Message

There are only two steps to log a message with Logback:

  1. Obtain a Logger object from org.slf4j.LoggerFactory.
  2. Log a message based on the requirements.

4.1 DemoLog

In this step, I will create an abstract class: DemoLog, which logs five parameterized messages, one for each log level respectively.

DemoLog.java

package jcg.zheng.demo.logbackdemo.component;

import org.slf4j.Logger;

public abstract class DemoLog {

public static void logStatements(Logger logger, String message) {
    logger.trace("log a trace message {}", message);
    logger.debug("log a debug message {}", message);
    logger.info("log a info message {}", message);
    logger.warn("log a warn message {}", message);
    logger.error("log an error message {}", message);
}

}

4.2 TestComponent1

I will create a Spring bean –TestComponent1, which extends from DemoLog. TestComponent1 has a Logger object created from org.slf4j.LoggerFactory‘s static method –getLogger. The logger name is set with TestComponent1 class’s name. It logs five messages in both @PostConstruct and @PreDestroy methods.

TestComponent1.java

package jcg.zheng.demo.logbackdemo.component;

import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;

@Component public class TestComponent1 extends DemoLog{

private static final Logger lOG = LoggerFactory.getLogger(TestComponent1.class);

@PostConstruct
public void setup() {
    DemoLog.logStatements(lOG, "-inside TestComponent1.setup() ");
}

@PreDestroy
public void clean() {
    DemoLog.logStatements(lOG,"-inside TestComponent1.clean() ");
}

}

4.3 TestComponent2

Repeat step 4.2 to create a TestComponent2 Spring bean.

TestComponent2.java

package jcg.zheng.demo.logbackdemo.component;

import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;

@Component public class TestComponent2 extends DemoLog{ private static final Logger lOG = LoggerFactory.getLogger(TestComponent2.class);

@PostConstruct
public void setup() {
    logStatements(lOG,"-inside TestComponent2.setup() ");
}

@PreDestroy
public void clean() {
    logStatements(lOG,"-inside TestComponent2.clean() ");
}

}

4.4 Demo

Execute java -jar target\logback-demo-0.0.1-SNAPSHOT.jar to start the LogbackDemoApplication. The log messages from both TestComponent1 and TestComponent2 will be placed in the system console.

application output

C:\gitworkspace\logback-demo>java -jar target\logback-demo-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _ /\ / _' __ _ ()_ __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ _)| |)| | | | | || (_| | ) ) ) ) ' |
| .|| ||| |_, | / / / / =========||==============|_/=//_// :: Spring Boot :: (v2.0.3.RELEASE)

2018-07-20 15:23:15.023 INFO 4480 --- [ main] j.z.d.l.LogbackDemoApplication : Starting LogbackDemoApplication v0.0.1-SNAPSHOT on SL2LS431841 with PID 4480 (C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar started by Shu.Shan in C:\gitworkspace\logback-demo) 2018-07-20 15:23:15.027 INFO 4480 --- [ main] j.z.d.l.LogbackDemoApplication : No active profile set, falling back to default profiles: default 2018-07-20 15:23:15.087 INFO 4480 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@38cccef: startup date [Fri Jul 20 15:23:15 CDT 2018]; root of context hierarchy 2018-07-20 15:23:15.751 INFO 4480 --- [ main] j.z.d.l.component.TestComponent2 : log a info message -inside TestComponent2.setup() 2018-07-20 15:23:15.752 WARN 4480 --- [ main] j.z.d.l.component.TestComponent2 : log a warn message -inside TestComponent2.setup() 2018-07-20 15:23:15.752 ERROR 4480 --- [ main] j.z.d.l.component.TestComponent2 : log an error message -inside TestComponent2.setup() 2018-07-20 15:23:15.757 INFO 4480 --- [ main] j.z.d.l.component.TestComponent1 : log a info message -inside TestComponent1.setup() 2018-07-20 15:23:15.757 WARN 4480 --- [ main] j.z.d.l.component.TestComponent1 : log a warn message -inside TestComponent1.setup() 2018-07-20 15:23:15.758 ERROR 4480 --- [ main] j.z.d.l.component.TestComponent1 : log an error message -inside TestComponent1.setup() 2018-07-20 15:23:16.199 INFO 4480 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-07-20 15:23:16.218 INFO 4480 --- [ main] j.z.d.l.LogbackDemoApplication : Started LogbackDemoApplication in 1.651 seconds (JVM running for 2.149) 2018-07-20 15:23:16.223 INFO 4480 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@38cccef: startup date [Fri Jul 20 15:23:15 CDT 2018]; root of context hierarchy 2018-07-20 15:23:16.225 INFO 4480 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2018-07-20 15:23:16.226 INFO 4480 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log a info message -inside TestComponent1.clean() 2018-07-20 15:23:16.226 WARN 4480 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log a warn message -inside TestComponent1.clean() 2018-07-20 15:23:16.226 ERROR 4480 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log an error message -inside TestComponent1.clean() 2018-07-20 15:23:16.226 INFO 4480 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log a info message -inside TestComponent2.clean() 2018-07-20 15:23:16.227 WARN 4480 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log a warn message -inside TestComponent2.clean() 2018-07-20 15:23:16.227 ERROR 4480 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log an error message -inside TestComponent2.clean()

C:\gitworkspace\logback-demo>

Note: The DEBUG and TRACE messages are not printed here.

4.5 Magic Revealed

In this step, I will explain how and why these log messages get displayed in the system console.

4.5.1 Where is the Logback Dependency?

Spring-boot-starter contains spring-boot-starter-logging. Spring-boot-starter-logging includes Logback’s three libraries: logback-core, logback-classic, and logback-access. You can confirm that by viewing the effective pom.xml with the steps below:

  1. Open Eclipse.
  2. Open logback-demo maven project.
  3. Open pom.xml.
  4. Click on the “Effective POM” tab and you can find all three libraries there.

4.5.2 Where is Logback Class?

Simple logging facade for Java (SLF4J) is a facade for logging frameworks. Logback, Java Util Logging, and Log4J2 are commonly used implementations of SLF4J. By writing against SLF4J, our code remains decoupled from Logback, therefore providing us the flexibility to plug-in a different logging framework if required.

As you saw in both TestComponent1 and TestComponent2, the Logger interface is imported from org.slf4j.Logger. It is not from ch.qos.logback.classic.Logger; Spring Boot chooses Logback.

4.5.3 Where is Logback Configuration?

Logback’s configuration file can be placed in the classpath and named as logback.xml or logback-test.xml. Logback first tries to find configuration by searching for files with the name logback-test.xml, logback.groovy, or logback.xml, in that specific order. If it does not find these files, it will attempt to locate an implementation of the com.qos.logback.classic.spi.Configurator class. If no configuration is found, then it defaults to a ConsoleAppender, and associates it with the root logger. The root logger level defaults to the DEBUG level.

Spring boot provides a default configuration file – base.xml. I copied the content here as a quick reference.

Spring boot base.xml

Spring boot defaults the root logger level to INFO. Click here for more details about the Spring default logging setting.

In this simple application, we did not set up any configurations, so Logback uses the default Spring boot configuration to output the INFO log message to the system console.

5. Logger Level

Logback has five levels, in order of precedence:

Applications use the logger’s level to filter the messages when placing them into an Appender.

5.1 TestComponent3

A Logger's Level can be set either via configuration or Logger.setLevel(). Setting the level in the code overrides the setting in the configuration files. The root logger defaults to DEBUG. If a Logger isn’t explicitly assigned a level, it inherits the level of its closest ancestor.

TestComponent3.java

package jcg.zheng.demo.logbackdemo.component;

import javax.annotation.PostConstruct;

import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;

import ch.qos.logback.classic.Level;

@Component public class TestComponent3 extends DemoLog{

@PostConstruct
public void setup() {
    ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(TestComponent3.class);
    logbackLogger.setLevel(Level.ERROR);
    DemoLog.logStatements(logbackLogger, "-inside TestComponent3.setup() ");
}

}

Note: The SLF4J’s Logger does not support setLevel(), so we have to cast it to ch.qos.logback.classic.Logger in order to set the level via code.

5.2 Demo

Execute java -jar target\logback-demo-0.0.1-SNAPSHOT.jar to start the LogbackDemoApplication.

application output

C:\gitworkspace\logback-demo>java -jar target\logback-demo-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _ /\ / _' __ _ ()_ __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ _)| |)| | | | | || (_| | ) ) ) ) ' |
| .|| ||| |_, | / / / / =========||==============|_/=//_// :: Spring Boot :: (v2.0.3.RELEASE)

2018-07-20 15:52:59.374 INFO 12116 --- [ main] j.z.d.l.LogbackDemoApplication : Starting LogbackDemoApplication v0.0.1-SNAPSHOT on SL2LS431841 with PID 12116 (C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar started by Shu.Shan in C:\gitworkspace\logback-demo) 2018-07-20 15:52:59.379 INFO 12116 --- [ main] j.z.d.l.LogbackDemoApplication : No active profile set, falling back to default profiles: default 2018-07-20 15:52:59.449 INFO 12116 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3ada9e37: startup date [Fri Jul 20 15:52:59 CDT 2018]; root of context hierarchy 2018-07-20 15:53:00.061 INFO 12116 --- [ main] j.z.d.l.component.TestComponent2 : log a info message -inside TestComponent2.setup() 2018-07-20 15:53:00.069 WARN 12116 --- [ main] j.z.d.l.component.TestComponent2 : log a warn message -inside TestComponent2.setup() 2018-07-20 15:53:00.069 ERROR 12116 --- [ main] j.z.d.l.component.TestComponent2 : log an error message -inside TestComponent2.setup() 2018-07-20 15:53:00.072 ERROR 12116 --- [ main] j.z.d.l.component.TestComponent3 : log an error message -inside TestComponent3.setup() 2018-07-20 15:53:00.076 INFO 12116 --- [ main] j.z.d.l.component.TestComponent1 : log a info message -inside TestComponent1.setup() 2018-07-20 15:53:00.077 WARN 12116 --- [ main] j.z.d.l.component.TestComponent1 : log a warn message -inside TestComponent1.setup() 2018-07-20 15:53:00.079 ERROR 12116 --- [ main] j.z.d.l.component.TestComponent1 : log an error message -inside TestComponent1.setup() 2018-07-20 15:53:00.608 INFO 12116 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-07-20 15:53:00.631 INFO 12116 --- [ main] j.z.d.l.LogbackDemoApplication : Started LogbackDemoApplication in 1.764 seconds (JVM running for 2.319) 2018-07-20 15:53:00.636 INFO 12116 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3ada9e37: startup date [Fri Jul 20 15:52:59 CDT 2018]; root of context hierarchy 2018-07-20 15:53:00.639 INFO 12116 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2018-07-20 15:53:00.642 INFO 12116 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log a info message -inside TestComponent1.clean() 2018-07-20 15:53:00.650 WARN 12116 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log a warn message -inside TestComponent1.clean() 2018-07-20 15:53:00.652 ERROR 12116 --- [ Thread-2] j.z.d.l.component.TestComponent1 : log an error message -inside TestComponent1.clean() 2018-07-20 15:53:00.653 INFO 12116 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log a info message -inside TestComponent2.clean() 2018-07-20 15:53:00.655 WARN 12116 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log a warn message -inside TestComponent2.clean() 2018-07-20 15:53:00.657 ERROR 12116 --- [ Thread-2] j.z.d.l.component.TestComponent2 : log an error message -inside TestComponent2.clean()

C:\gitworkspace\logback-demo>

Note: TestComponent3 only prints out ERROR logs regardless of the default INFO setting.

6. Appender

Loggers pass LoggingEvents to Appenders. Logback-core provides several useful appenders.

6.1 ConsoleAppender

We’ve seen ConsoleAppender already. ConsoleAppender appends messages to System.out or System.err.

6.2 FileAppender

FileAppender appends messages to a file. It supports a broad range of configuration parameters. FileAppender is configured with a file name via <file>. The <append> tag instructs the Appender to append messages to existing file rather than truncating it.

6.3 RollingFileAppender

RollingFileAppender appends log messages to the file and the file is “rolled” based on time, log file size, or a combination of both.

7. Layout

Layout is responsible for transforming an incoming LoggingEvents into a String. PatternLayout is included in Logback. It implements a large variety of conversion words and format modifiers for creating patterns. It recognizes conversion words with a %. Here are some commonly used conversion patterns:

8. Configuration Examples

In this step, I will demonstrate the Logback configuration in four ways:

8.1 Application.properties

In this step, I will add the logging configuration in the appplication.properties to output the log message with different formats for both console and file appenders.

application.properties

logging.level.=INFO

logging.level.org.springframework=OFF logging.level.jcg.zheng.demo=DEBUG logging.file=demo-sb-default.log

Logging pattern for the console

logging.pattern.console= %date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", CST} CST - %msg%n

Logging pattern for file

logging.pattern.file= %date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", UTC} UTC [%thread] %-5level %logger{36} - %msg%n

Execute java -jar target\logback-demo-0.0.1-SNAPSHOT.jar to start the LogbackDemoApplication.

Application Console Output

C:\gitworkspace\logback-demo>java -jar target\logback-demo-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _ /\ / _' __ _ ()_ __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ _)| |)| | | | | || (_| | ) ) ) ) ' |
| .|| ||| |_, | / / / / =========||==============|_/=//_// :: Spring Boot :: (v2.0.3.RELEASE)

2018-07-20T16:03:00,180-05:00 CST - Starting LogbackDemoApplication v0.0.1-SNAPSHOT on SL2LS431841 with PID 11904 (C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar started by Shu.Shan in C:\gitworkspace\logback-demo) 2018-07-20T16:03:00,184-05:00 CST - Running with Spring Boot v2.0.3.RELEASE, Spring v5.0.7.RELEASE 2018-07-20T16:03:00,185-05:00 CST - No active profile set, falling back to default profiles: default 2018-07-20T16:03:00,728-05:00 CST - log a debug message -inside TestComponent1.setup() 2018-07-20T16:03:00,729-05:00 CST - log a info message -inside TestComponent1.setup() 2018-07-20T16:03:00,729-05:00 CST - log a warn message -inside TestComponent1.setup() 2018-07-20T16:03:00,729-05:00 CST - log an error message -inside TestComponent1.setup() 2018-07-20T16:03:00,731-05:00 CST - log a debug message -inside TestComponent2.setup() 2018-07-20T16:03:00,732-05:00 CST - log a info message -inside TestComponent2.setup() 2018-07-20T16:03:00,732-05:00 CST - log a warn message -inside TestComponent2.setup() 2018-07-20T16:03:00,732-05:00 CST - log an error message -inside TestComponent2.setup() 2018-07-20T16:03:00,734-05:00 CST - log an error message -inside TestComponent3.setup() 2018-07-20T16:03:01,234-05:00 CST - Started LogbackDemoApplication in 1.57 seconds (JVM running for 2.102) 2018-07-20T16:03:01,240-05:00 CST - log a debug message -inside TestComponent2.clean() 2018-07-20T16:03:01,241-05:00 CST - log a info message -inside TestComponent2.clean() 2018-07-20T16:03:01,241-05:00 CST - log a warn message -inside TestComponent2.clean() 2018-07-20T16:03:01,242-05:00 CST - log an error message -inside TestComponent2.clean() 2018-07-20T16:03:01,242-05:00 CST - log a debug message -inside TestComponent1.clean() 2018-07-20T16:03:01,243-05:00 CST - log a info message -inside TestComponent1.clean() 2018-07-20T16:03:01,243-05:00 CST - log a warn message -inside TestComponent1.clean() 2018-07-20T16:03:01,245-05:00 CST - log an error message -inside TestComponent1.clean()

C:\gitworkspace\logback-demo>

Here are the demo-db-default.log file contents.

Application File Output

2018-07-20T21:03:00,180Z UTC [main] INFO j.z.d.l.LogbackDemoApplication - Starting LogbackDemoApplication v0.0.1-SNAPSHOT on SL2LS431841 with PID 11904 (C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar started by Shu.Shan in C:\gitworkspace\logback-demo) 2018-07-20T21:03:00,184Z UTC [main] DEBUG j.z.d.l.LogbackDemoApplication - Running with Spring Boot v2.0.3.RELEASE, Spring v5.0.7.RELEASE 2018-07-20T21:03:00,185Z UTC [main] INFO j.z.d.l.LogbackDemoApplication - No active profile set, falling back to default profiles: default 2018-07-20T21:03:00,728Z UTC [main] DEBUG j.z.d.l.component.TestComponent1 - log a debug message -inside TestComponent1.setup() 2018-07-20T21:03:00,729Z UTC [main] INFO j.z.d.l.component.TestComponent1 - log a info message -inside TestComponent1.setup() 2018-07-20T21:03:00,729Z UTC [main] WARN j.z.d.l.component.TestComponent1 - log a warn message -inside TestComponent1.setup() 2018-07-20T21:03:00,729Z UTC [main] ERROR j.z.d.l.component.TestComponent1 - log an error message -inside TestComponent1.setup() 2018-07-20T21:03:00,731Z UTC [main] DEBUG j.z.d.l.component.TestComponent2 - log a debug message -inside TestComponent2.setup() 2018-07-20T21:03:00,732Z UTC [main] INFO j.z.d.l.component.TestComponent2 - log a info message -inside TestComponent2.setup() 2018-07-20T21:03:00,732Z UTC [main] WARN j.z.d.l.component.TestComponent2 - log a warn message -inside TestComponent2.setup() 2018-07-20T21:03:00,732Z UTC [main] ERROR j.z.d.l.component.TestComponent2 - log an error message -inside TestComponent2.setup() 2018-07-20T21:03:00,734Z UTC [main] ERROR j.z.d.l.component.TestComponent3 - log an error message -inside TestComponent3.setup() 2018-07-20T21:03:01,234Z UTC [main] INFO j.z.d.l.LogbackDemoApplication - Started LogbackDemoApplication in 1.57 seconds (JVM running for 2.102) 2018-07-20T21:03:01,240Z UTC [Thread-2] DEBUG j.z.d.l.component.TestComponent2 - log a debug message -inside TestComponent2.clean() 2018-07-20T21:03:01,241Z UTC [Thread-2] INFO j.z.d.l.component.TestComponent2 - log a info message -inside TestComponent2.clean() 2018-07-20T21:03:01,241Z UTC [Thread-2] WARN j.z.d.l.component.TestComponent2 - log a warn message -inside TestComponent2.clean() 2018-07-20T21:03:01,242Z UTC [Thread-2] ERROR j.z.d.l.component.TestComponent2 - log an error message -inside TestComponent2.clean() 2018-07-20T21:03:01,242Z UTC [Thread-2] DEBUG j.z.d.l.component.TestComponent1 - log a debug message -inside TestComponent1.clean() 2018-07-20T21:03:01,243Z UTC [Thread-2] INFO j.z.d.l.component.TestComponent1 - log a info message -inside TestComponent1.clean() 2018-07-20T21:03:01,243Z UTC [Thread-2] WARN j.z.d.l.component.TestComponent1 - log a warn message -inside TestComponent1.clean() 2018-07-20T21:03:01,245Z UTC [Thread-2] ERROR j.z.d.l.component.TestComponent1 - log an error message -inside TestComponent1.clean()

Note: The console log messages display as CST timezone and the file log messages display as UTC timezone.

8.2 Logback-spring.xml

In this step, I will add a logback-spring.xml file to log the messages into a logs_sb_logback directory.

logback-spring.xml

${FOLDER_HOME}/app_info.log %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${FOLDER_HOME}/app_info.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<appender name="FILE_ERROR"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${FOLDER_HOME}/app_error.log</file>
    <encoder
        class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss};%msg%n
        </Pattern>
    </encoder>

    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${FOLDER_HOME}/app_error.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent1" level="info"
    additivity="false">
    <appender-ref ref="FILE_INFO" />
</logger>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent2" level="error"
    additivity="false">
    <appender-ref ref="FILE_ERROR" />
</logger>

<root level="info">
    <appender-ref ref="FILE_INFO" />
    <appender-ref ref="FILE_ERROR" />
</root>

Execute java -jar target\logback-demo-0.0.1-SNAPSHOT.jar to start the LogbackDemoApplication. Then you will see two log files created under the logs_sb_logback directory and no log messages in the system console.

8.3 Logback.xml

In this step, I will add a logback.xml file to log the messages into a logs-lb directory.

logback.xml

${LOG_DIR}/app_info.log %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOG_DIR}/app_info.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<appender name="FILE_ERROR"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_DIR}/app_error.log</file>
    <encoder
        class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss};%msg%n
        </Pattern>
    </encoder>

    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOG_DIR}/app_error.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent1" level="info"
    additivity="false">
    <appender-ref ref="FILE_INFO" />
</logger>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent2" level="error"
    additivity="false">
    <appender-ref ref="FILE_ERROR" />
</logger>

<root level="info">
    <appender-ref ref="FILE_INFO" />
    <appender-ref ref="FILE_ERROR" />
</root>

Execute java -jar target\logback-demo-0.0.1-SNAPSHOT.jar to start the LogbackDemoApplication. You will see two log files created at the logs_lb directory.

8.4 Logback-test.xml

In this step, I will create a logback-test.xml to output the log messages at logs-test folder. Setting additivity to false disables the default behavior; Setting debug to true enable the Logback debugging messages.

Logback-test.xml

${LOG_DIR}/app_info.log %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOG_DIR}/app_info.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<appender name="FILE_ERROR"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_DIR}/app_error.log</file>
    <encoder
        class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss};%msg%n
        </Pattern>
    </encoder>

    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOG_DIR}/app_error.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent1" level="info"
    additivity="false">
    <appender-ref ref="FILE_INFO" />
</logger>

<logger name="jcg.zheng.demo.logbackdemo.component.TestComponent2" level="error"
    additivity="false">
    <appender-ref ref="FILE_ERROR" />
</logger>

<root level="info">
    <appender-ref ref="FILE_INFO" />
    <appender-ref ref="FILE_ERROR" />
</root>

Execute mvn clean install.

Logback-test.xml

C:\gitworkspace\logback-demo>mvn clean install Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0 [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building logback-demo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ logback-demo --- [INFO] Deleting C:\gitworkspace\logback-demo\target [INFO] [INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ logback-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ logback-demo--- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to C:\gitworkspace\logback-demo\target\classes [INFO] [INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @logback-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ logback-demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:\gitworkspace\logback-demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ logback-demo --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running jcg.zheng.demo.logbackdemo.LogbackDemoApplicationTests16:28:34,407 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:/C:/gitworkspace/logback-demo/target/test-classes/logback-test.xml] 16:28:34,517 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/C:/gitworkspace/logback-demo/target/test-classes/logback-test.xml] 16:28:34,517 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 2 minutes 16:28:34,521 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]

16:28:34,527 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [FILE_INFO] 16:28:34,582 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@603650290 - Nocompression will be used 16:28:34,584 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@603650290 - Will use the pattern logs-test/app_info.%d{yyyy-MM-dd}.%i.log for the active file 16:28:34,588 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@396f6598 - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs-test/app_info.%d{yyyy-MM-dd}.%i.log'. 16:28:34,588 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@396f6598 - Roll-over at midnight. 16:28:34,595 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@396f6598 - Setting initial period to Fri Jul 20 16:27:48 CDT 2018 16:28:34,596 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@396f6598 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead 16:28:34,596 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@396f6598 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy 16:28:34,601 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_INFO] - Active log file name: logs-test/app_info.log 16:28:34,601 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_INFO] - File property is set to [logs-test/app_info.log] 16:28:34,603 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]

16:28:34,604 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [FILE_ERROR] 16:28:34,606 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@674483268 - Nocompression will be used 16:28:34,607 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@674483268 - Will use the pattern logs-test/app_error.%d{yyyy-MM-dd}.%i.log for the active file 16:28:34,607 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@33f88ab - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs-test/app_error.%d{yyyy-MM-dd}.%i.log'. 16:28:34,607 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@33f88ab - Roll-over at midnight. 16:28:34,609 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@33f88ab - Setting initial period to Fri Jul 20 16:27:48 CDT 2018 16:28:34,609 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@33f88ab - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead 16:28:34,609 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@33f88ab - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy 16:28:34,610 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_ERROR] - Active log file name: logs-test/app_error.log 16:28:34,611 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_ERROR] - File property is set to [logs-test/app_error.log] 16:28:34,613 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [jcg.zheng.demo.logbackdemo.component.TestComponent1] to INFO 16:28:34,613 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [jcg.zheng.demo.logbackdemo.component.TestComponent1] tofalse 16:28:34,613 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_INFO] to Logger[jcg.zheng.demo.logbackdemo.component.TestComponent1] 16:28:34,614 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [jcg.zheng.demo.logbackdemo.component.TestComponent2] to ERROR

16:28:34,615 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [jcg.zheng.demo.logbackdemo.component.TestComponent2] tofalse 16:28:34,615 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_ERROR] to Logger[jcg.zheng.demo.logbackdemo.component.TestComponent2] 16:28:34,615 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 16:28:34,615 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_INFO] to Logger[ROOT] 16:28:34,615 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_ERROR] to Logger[ROOT] 16:28:34,616 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 16:28:34,617 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@e320068 -Registering current configuration as safe fallback point 16:28:35,309 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/C:/gitworkspace/logback-demo/target/test-classes/logback-test.xml] 16:28:35,309 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 2 minutes 16:28:35,310 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]

16:28:35,310 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [FILE_INFO] 16:28:35,314 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1261031890 - No compression will be used 16:28:35,314 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1261031890 - Will use the pattern logs-test/app_info.%d{yyyy-MM-dd}.%i.log for the active file 16:28:35,315 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f485fda - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs-test/app_info.%d{yyyy-MM-dd}.%i.log'. 16:28:35,315 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f485fda - Roll-over at midnight. 16:28:35,317 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f485fda - Setting initial period to Fri Jul 20 16:28:35 CDT 2018 16:28:35,317 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f485fda - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead 16:28:35,317 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f485fda - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy 16:28:35,318 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_INFO] - Active log file name: logs-test/app_info.log 16:28:35,318 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_INFO] - File property is set to [logs-test/app_info.log] 16:28:35,319 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]

16:28:35,320 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [FILE_ERROR] 16:28:35,322 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@673586830 - Nocompression will be used 16:28:35,322 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@673586830 - Will use the pattern logs-test/app_error.%d{yyyy-MM-dd}.%i.log for the active file 16:28:35,323 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@d737b89 - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs-test/app_error.%d{yyyy-MM-dd}.%i.log'. 16:28:35,323 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@d737b89 - Roll-over at midnight. 16:28:35,324 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@d737b89 - Setting initial period to Fri Jul 20 16:28:35 CDT 2018 16:28:35,324 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@d737b89 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead 16:28:35,325 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@d737b89 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy 16:28:35,326 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_ERROR] - Active log file name: logs-test/app_error.log 16:28:35,326 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE_ERROR] - File property is set to [logs-test/app_error.log] 16:28:35,327 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [jcg.zheng.demo.logbackdemo.component.TestComponent1] to INFO 16:28:35,328 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating INFO level on Logger[jcg.zheng.demo.logbackdemo.component.TestComponent1] onto the JUL framework 16:28:35,329 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [jcg.zheng.demo.logbackdemo.component.TestComponent1] tofalse 16:28:35,329 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_INFO] to Logger[jcg.zheng.demo.logbackdemo.component.TestComponent1] 16:28:35,329 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [jcg.zheng.demo.logbackdemo.component.TestComponent2] to ERROR

16:28:35,329 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating ERROR level on Logger[jcg.zheng.demo.logbackdemo.component.TestComponent2] onto the JUL framework 16:28:35,330 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [jcg.zheng.demo.logbackdemo.component.TestComponent2] tofalse 16:28:35,330 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_ERROR] to Logger[jcg.zheng.demo.logbackdemo.component.TestComponent2] 16:28:35,330 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 16:28:35,330 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating INFO level on Logger[ROOT] onto the JUL framework 16:28:35,330 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_INFO] to Logger[ROOT] 16:28:35,331 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE_ERROR] to Logger[ROOT] 16:28:35,331 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 16:28:35,331 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@72cc7e6f - Registering current configuration as safe fallback point 16:28:35,387 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating OFF level on Logger[org.springframework] onto the JUL framework 16:28:35,388 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating DEBUG level on Logger[jcg.zheng.demo] onto the JUL framework

. ____ _ __ _ _ /\ / _' __ _ ()_ __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ _)| |)| | | | | || (_| | ) ) ) ) ' |
| .|| ||| |_, | / / / / =========||==============|_/=//_// :: Spring Boot :: (v2.0.3.RELEASE)

16:28:36,377 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@8519cb4- Propagating ERROR level on Logger[jcg.zheng.demo.logbackdemo.component.TestCom ponent3] onto the JUL framework [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.728 s - in jcg.zheng.demo.logbackdemo.LogbackDemoApplicationTests [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ logback-demo --- [INFO] Building jar: C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ logback-demo --- [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ logback-demo --- [INFO] Installing C:\gitworkspace\logback-demo\target\logback-demo-0.0.1-SNAPSHOT.jar to C:\repo\jcg\zheng\demo\logback-demo\0.0.1-SNAPSHOT\logback-demo-0.0.1-SNAPSHOT.jar [INFO] Installing C:\gitworkspace\logback-demo\pom.xml to C:\repo\jcg\zheng\demo\logback-demo\0.0.1-SNAPSHOT\logback-demo-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.836s [INFO] Finished at: Fri Jul 20 16:28:39 CDT 2018 [INFO] Final Memory: 26M/368M [INFO] ------------------------------------------------------------------------ C:\gitworkspace\logback-demo>

Here are the log files in this simple application after ran for two days.

Logback Tutorial - Log Files

Figure 1 Log Files

9. Summary

In this article, we covered the fundamental functions of using Logback in a Spring boot Java application. We looked at the Logback’s architecture components: loggers, appenders, and layouts. We also demonstrated four configuration examples to create, format, and filter log messages.

10. Download the Source Code

This example consists of a Spring boot application to demonstrate the Logback features.