Chapter 1 Introduction to logback

Posted by northstjarna on Mon, 07 Mar 2022 17:25:10 +0100

What is logback

logback inherits from log4j and is based on a logging system with ten years of industrial experience. It is faster and smaller than all other logging systems and contains many unique and useful features.

The first step of genius

requirement

The logback classic module needs to add slf4j API in the classpath jar,logback-core.jar and logback classic jar.

Example 1.1: Basic template for logging

package chapters.introduction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld1 {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1");
        logger.debug("hello world");
    }
}

The class HelloWorld1 is defined in the package chapters In the introduction, it imports two classes, Logger and LoggerFactory, which are defined in the SLF4J API at org Slf4j package.

In this example, main() contains a DEBUG level log statement, and the output information is "hello world"

Running HelloWord1 will see a line of log in the console. Because of the default configuration policy of logback: when there is no default configuration, logback will add a ConsoleAppender in the root logger

11:58:56.662 [main] DEBUG chapters.introduction.HelloWorld1 - hello world

Logback reports its own status information through an internal status system. Events occurring in the logback lifecycle can be obtained through the StatusManager.

Example: Printing Logger Status

package chapters.introduction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;

public class HelloWorld2 {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");
        logger.debug("Hello world");
        
        // Print internal status
        LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
        StatusPrinter.print(lc);
    }
}

The operation results are as follows:

12:23:49.324 [main] DEBUG chapters.introduction.HelloWorld2 - Hello world
12:23:49,258 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
12:23:49,258 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
12:23:49,258 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
12:23:49,262 |-INFO in ch.qos.logback.classic.BasicConfigurator@2e5d6d97 - Setting up default configuration.

Logback did not find logback test XML and logback XML configuration file, so it is configured through the default configuration policy - adding a basic console provider. The Appender class is seen as the destination of the output. Appenders include console, files, Syslog, TCP Sockets, JMS and other log output destinations. Users can easily create their own Appender according to their own situation.

If an error occurs, logback will automatically print its internal status information on the console.

The example given earlier is relatively simple. In fact, in large applications, logging won't make much difference. The general form of logging will not change, but the configuration method will be different. You may want to configure logback according to your own needs. How to configure logback will be described in the next chapter.

In the above example, we use statusprinter Print () prints the internal state of logback itself. The internal state of logback is very useful for finding problems related to logback.

Log back can be enabled in three steps:

  1. Configure the logback environment. You can do it in simple or complex ways, which will be described later.
  2. If you want to print logs in each class, you need to take the full name of the current class or the current class as a parameter and call org slf4j. LoggerFactory. Getlogger () method.
  3. Use the instance logger to call different methods to print the log. Example: debug(), info(), warn(), error(). Through these methods, the log will be output in the configured appender.

Build logback

logback uses Maven as the build tool.

If you install maven, you can run mvn install in the logback decompression folder to build logback and the modules it contains. Maven will automatically download other class libraries required by logback.

Logback's compressed package contains the complete source code, so you can change it at will according to your needs. And as long as you use LGPL and EPL protocol, you can even release your changed version.

Topics: Logback