Logback of Java logging system

Table of Contents

Logback

Simple use of Logback

Logback configuration file

log4j.peoperties converted to logback.xml


Logback

Logback’s performance is better than log4j.

Logback is divided into three modules:

  • logback-core: the basic module of the other two modules
  • logback-classic: It is an improved version of log4j and also implements the slf4j API
  • logback-access: The access module is integrated with the Servlet container to provide the function of accessing logs through HTTP (basically not used)

Simple use of Logback

Introduce dependencies

 <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
public class TestLogback {

    public final static Logger logger = LoggerFactory.getLogger(TestLogback.class);

    @Test
    public void testLog() throws Exception {
        logger.error("error");
        logger.warn("wring");
        logger.info("info");
        logger.debug("debug");
        logger.trace("trace");
    }
}

Logback configuration file

Logback will read the following types of configuration files in sequence

  • logback.groovy
  • logback-test.xml
  • logback.xml

If neither exists, the default configuration will be used.

The following is how the xml format is written

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--
    Configure centralized management properties
    We can directly change the value of the attribute
    Format:$(name}
    -->
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %M %L [%thread] %m%n"></property>
    <property name="log_dir" value="D:/logs"></property>
    <!--
    Log output format:
        %-5level log level
        %d{yyyy-MM-dd HH:mm:ss} date
        %cThe full name of the class
        %M is method
        %L is the line number
        %thread thread name
        %m or %msg is information
        %nLine break
    -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!--Control the output stream object, the default is System.out black font-->
        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </appender>

    <appender name="file" class="ch.qos.logback.core.FileAppender">
        <!--Specify the save path-->
        <file>${log_dir}/logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </appender>

      <!--html format log file output appender-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <!--Specify the save path-->
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss} %M %L %thread %m%n</pattern>
            </layout>
        </encoder>
    </appender>

      <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log_dir}/roll_logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
        <!--Specify splitting rules-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--Split by time format-->
            <fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd} %i.log</fileNamePattern>
            <!--Split by file size-->
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
        <!--Log filtering rules-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
          <!--Only output error level logs-->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!--Asynchronous logging to improve performance-->
    <appender name="async" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="rollFile"></appender-ref>
    </appender>
    <!--root Logger configuration-->
    <root level="ALL">
        <appender-ref ref="console"></appender-ref>
        <appender-ref ref="file"></appender-ref>
      <appender-ref ref="htmlFile"></appender-ref>
    </root>
</configuration>

The output after using the configuration file is as shown below.

log4j.peoperties converted to logback.xml

Official conversion address https://logback.qos.ch/translator/services/xml2Canon.html

converted result

It should be noted that the meaning of some symbols in log4j’s log format is different from that of logback. You need to check it before using it.