Log framework commonly used in Java System

Overview of logging technology


Diary in life: a diary in life is like a diary, which can record every bit of your life.
Log in the program: the log in the program can be used to record the information during the operation of the program and can be permanently stored.

Advantages of log technology:

The information executed by the system can be selectively recorded to the specified location (console, file and database).
You can control whether to record the log in the form of switch at any time without modifying the source code.

What are the specific advantages of logging technology?

Log technology architecture

Log specification: some interfaces that provide standards for the design of log implementation framework.
Logging framework: the implementation code of logging that has been done by cattle people or third-party companies, which can be used by latecomers directly. Some people started SLF4J because they were not satisfied with the interface of common logging. Some people started Logback because they were not satisfied with the performance of Log4j.

Question 1: what is the specification of log? There are several common forms.

Most of the log specifications are interfaces provided to the implementation framework for design.

Question 2: common specifications are:

Commons Logging
Simple Logging Facade for Java

Question 3: what are the common implementation frameworks of logging?

Logback(We focus on learning, and others are similar)

Logback overview

Logback log framework

Logback By log4j Another open source log component designed by the founder has better performance than log4j Better;
Official website: https://logback.qos.ch/index.html
Logback Is based on slf4j The framework of log specification implementation.

Logback is mainly divided into three technical modules:
1. Logback core: the logback core module lays the foundation for the other two modules and must have.
2. Logback classic: it is an improved version of log4j, and it fully implements the slf4j API.
3. The logback access module is integrated with Servlet containers such as Tomcat and Jetty to provide HTTP access log function.

Logback quick start

Requirement: import Logback logging technology into the project to record the log information of the system
Note: logback_classic-1.2.3.jar, logback-core-1.2.3.jar, slf4j-api-1.7.26.jar and logback The XML address is:, you can also find it on the Internet.
① : create a new folder lib under the project, import the relevant jar package of Logback into this folder, and add it to the project dependency library.
② : the core configuration file of Logback XML is directly copied to the src directory (it must be under src).
③ : the object that gets the log in the code
public static final Logger LOGGER = LoggerFactory.getLogger("class object"); The constant method is used here
④ : use the log object LOGGER to call its method to output log information that cannot be

Logback configuration details - output location and format settings

The features of the Logback logging system are through the core configuration file Logback XML controlled.
Logback log output location and format settings:

adopt logback.xml Medium<append>The tag can set the detailed format of output location and log information.
You can usually set two log output locations: one is the console and the other is in the system file

Configuration flags output to console:
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

Configuration flags output to system files:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

logback.xml explanation:

<?xml version="1.0" encoding="UTF-8"?>
        CONSOLE(console 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 Change to System.err(Print (red)-->
            <!--Format output:
                %d%d{yyyy-MM-dd HH:mm:ss.SSS}: Indicates the date(2022-04-15 13:37:38.600)
                %-5level: The level is displayed 5 characters wide from the left([INFO ], [TRACE])
                %c: Class name( Test.class)
                %thread: Represents the thread name([main])
                %msg: Log message;
                %n: Is a newline character
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level]  %c [%thread] : %msg%n</pattern>

        appender Medium name Where are the naming settings printed(name = "FILE"Is printed in a file, name = "CONSOLE"Is printed in the console)
        File(file): Is the direction of the 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 how to split the file 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(Print all logs) and OFF(Close, do not print log)
   , default debug
    <root>Can contain zero or more<appender-ref>Element to identify the output location, which will be controlled by this log level.
    <root level="all">
        <appender-ref ref="CONSOLE"/>  <!-- Association: it can be output on the console. Without this item, it will not be output on the console-->
        <appender-ref ref="FILE"/>

Logback configuration details - log level settings

log level

The levels are: TRACE< DEBUG< INFO<WARN<ERROR  ; 
The default level is debug(Ignore case), corresponding to its method.
Function: it is used to control which log levels in the system can be output. Only log information with a level not lower than the set level is output.
ALL  and  OFF Open all log information and close all log information.

Specifically, set the log level in the level attribute of the < root level = "INFO" > tag

<root level="INFO">   
	 <appender-ref ref="CONSOLE"/>   
	  <appender-ref ref="FILE" />

Question 1. What is the function of setting the log output level?

It is used to control which log levels in the system can be output.

Question 2. What is the log level of Logback?

 The default level is debug(Ignore case), and only output logs that are not lower than the current level
ALL  and OFF Open all logs and close all logs respectively

Tags: Java

Posted by Lol5916 on Sat, 16 Apr 2022 18:14:28 +0930