Java log framework Logback construction and basic use

Logging framework Logback

Introduction to Logback

Logback logging framework :

Logback is another open source log component designed by the founder of log4j, with better performance than log4j

Official website: https://logback.qos.ch/index.html

Logback is a framework implemented based on slf4j's log specification.

Logback is mainly divided into the following three technical modules

logback-core: The logback-core module lays the foundation for the other two modules and is a must have.

logback-classic: It is an improved version of log4j, and it fully implements the slf4j API.

The logback-access module integrates with Servlet containers such as Tomcat and Jetty to provide HTTP access logging functionality

Logback build

Purpose: Import Logback log technology into the project to record system log information

The steps are as follows :

  1. Create a new folder lib under the project, and import the relevant jar packages of Logback into this folder.

insert image description here

  1. And add three jar packages to the project dependency library

insert image description here

insert image description here

  1. Copy the logback.xml core configuration file of Logback directly to the src directory (must be under src).

insert image description here

  1. Get log object in code
public class Test {
    
    
    // 创建Logback日志对象
    public static final Logger LOGGER = LoggerFactory.getLogger("Test.class");
}
  1. Use the log object LOGGER to call its method to output log information
public class Test {
    
    
    // 创建Logback日志对象
    public static final Logger LOGGER = LoggerFactory.getLogger("Test.class");
    public static void main(String[] args) {
    
    
        LOGGER.debug("main方法开始执行了");
        LOGGER.info("我开始记录第二行日志");
    }
}

Detailed Logback configuration

The features of the Logback logging system are controlled through the core configuration file logback.xml.

Logback log output location and format settings :

The output location and the detailed format of the log information can be set through the <append> tag in logback.xml.

Usually you can set two log output locations: one is the console and the other is in the system file

Configuration flags output to the console :

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

<!--
    CONSOLE :表示当前的日志信息是可以输出到控制台的。
-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!--输出流对象 默认 System.out 改为 System.err-->
    <target>System.out</target>
    <encoder>
        <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:日志级别从左显示5个字符宽度
            %msg:日志消息,%n是换行符-->
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level]  %c [%thread] : %msg%n</pattern>
    </encoder>
</appender>

Configuration flags output to system file :

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

<!-- File是输出的方向通向文件的 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        <charset>utf-8</charset>
    </encoder>
    <!--日志输出路径-->
    <file>/Users/chenyq/Documents/learn_Java/code/chenyq-data.log</file>
    <!--指定日志文件拆分和压缩规则-->
    <rollingPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!--通过指定压缩文件名称,来确定分割文件方式-->
        <fileNamePattern>/Users/chenyq/Documents/learn_Java/code/chenyq-data2-%d{yyyy-MMdd}.log%i.gz</fileNamePattern>
        <!--文件拆分大小-->
        <maxFileSize>1MB</maxFileSize>
    </rollingPolicy>
</appender>

Log level configuration :

If you only want to record some wrong log information or do not want to record logs after the system goes online, you can control which log information to output or not to output by setting the log output level.

log level :

The order of levels is: TRACE< DEBUG< INFO<WARN<ERROR ; the default level is debug (ignoring case), corresponding to its method.

Function: It is used to control which log levels in the system can be output, and only output log information whose level is not lower than the set level.

ALL and OFF are to open all log information and close all log information respectively.

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

<!-- 表示只打印INFO和INFO以上级别的 -->
<root level=“INFO">
  <appender-ref ref="CONSOLE"/>
  <appender-ref ref="FILE" />
</root>

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/127533853