Log technology (Logback)

During our development process, if we want to know the running information and details of the program clearly, we can use the log to record at this time.

Before learning log technology, the method of recording logs is the output statement of the system.

Advantages of logs compared to output statements:


If you use the output statement to record log information, when you don’t need some log information, you need to find the information that does not need to be printed in thousands of lines or even tens of thousands of lines of code, which is very troublesome, so the introduction log technology.  

The architecture of the log:

Among them, the specification interface is the specification standard provided for log implementation. The earliest implementation framework is Log4j, and the birth of Logback is because some people are not satisfied with the performance of slf4j, so the Logback implementation framework was created.

Logback core configuration file XML file code:

<?xml version="1.0" encoding="UTF-8"?>
        CONSOLE : Indicates that the current log information can be output to the console.
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <!--output stream object default System.out changed to System.err-->
            <!--Formatted output:%d represents the date,%thread Indicates the thread name,%-5level: Levels are displayed 5 characters wide from the left
                %msg: log messages,%n is a line break-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level]  %c [%thread] : %msg%n</pattern>

    <!-- File is the direction of output leading to the file -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        <!--log output path-->
        <!--Specify log file splitting and compression rules-->
            <!--Determine the split file method by specifying the name of the compressed file-->
            <!--file split size-->


    level:Used to set the printing level, regardless of case: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF
   , default debug
    <root>can contain zero or more<appender-ref>element, indicating that the output location will be controlled by this log level.
    <root level="ALL">
        <!-- If the location of printing is configured here, he may not print log information  -->
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE" />

Configuration of the output console format in the xml file

The format configuration of the log output to the file in the xml file

If you only want to record error log information or do not want to record log information, it involves the issue of log level. The following is the division of log level.

The Level here records the log level that needs to be output. When you select a log level, the log information lower than the log level will not be output. Next, you need to configure the associated printing location to print on the console or in a file.

The process of using the Logback log framework:

First, import the relevant jar package into the lib folder, then right-click the lib folder, click Add as libiray, and second, import the Logback core configuration file logback.xml to the src directory. The third step is to obtain the log object, which must be constant.

package com.itheima.logback;

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

public class Test {
    //Create a Logback object
    public static final Logger LOGGER =  LoggerFactory.getLogger("Test.class");
    public static void main(String[] args) {
        try {
            LOGGER.debug("main The method starts executing~~~");
            LOGGER.info("I'm starting to log the second line, I'm going to start doing the division");
            int a = 10;
            int b = 0;
            LOGGER.trace("a=" + a );
            LOGGER.trace("b=" + b );
        } catch (Exception e) {
            LOGGER.error("Abnormal function" + e);

prints in the console:

print in file:




Tags: Java Logback

Posted by CBR on Fri, 06 Jan 2023 16:58:26 +0530