LOGBACK use and detailed explanation is not perfect version

Understand what LOGBACK is

  1. Official website : http://logback.qos.ch
  2. logback-core: the basic module of the other two modules
  3. logback-classic: It is an improved version of log4j, and it fully implements the slf4j API so that you can easily change to other logging systems such as log4j or JDK14 Logging
  4. logback-access: The access module integrates with the Servlet container to provide the function of accessing logs through Http

Use of LOGBACK

Configuration of pom.xml

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.5</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
	<version>1.0.11</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
	<version>1.0.11</version>
</dependency>

The use and detailed explanation of LOGBACK.XML

Basic information of configuration

configuration

  • configuration
    • scan : The difference between true/false is that if the configuration file is changed, it will be reloaded. The default value is true
    • scanPeriod : monitor configuration file modify time
    • debug : If the property is set to true, the internal log information of logback will be printed, and the running status of logback can be viewed in real time. The default value is false
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="1800 seconds"
               debug="false">

<!-- <property/> -->
<!-- <timestamp/> -->
<!-- <appender"></appender>/> -->
<!-- <appender"></appender>/> -->
<!-- <root level="INFO"></root> -->

</configuration>

xml basic parameters

  • property
    • name,value: The defined value will be inserted into the logger context, using "${}" to use the variable
  • timestamp
    • datePattern: Set the mode of converting the current time (the time of parsing the configuration file) into a string, following the format of java.txt.SimpleDateFormat
<property name="log_dir" value="log-server" />
<property name="maxHistory" value="30" />
<timestamp key="byDay" datePattern="yyyy-MM-dd" />
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>INFO</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>NEUTRAL</onMismatch>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- 按天回滚 daily -->
        <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/all.%i.log
        </fileNamePattern>
        <!-- 日志最大的历史 6天 -->
        <maxHistory>${maxHistory}</maxHistory>
        <maxFileSize>100MB</maxFileSize>
    </rollingPolicy>
    <encoder>
        <pattern>
        %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

appender

appender : class type

Name description
ConsoleAppender To output the log to the console, there are the following sub-nodes: encoder: format the log. target: String System.out
FileAppender file : The name of the file to be written, which can be a relative directory or an absolute directory. If the parent directory does not exist, it will be created automatically. There is no default value. append : If it is true, the log is appended to the end of the file, if it is false, the existing file is cleared, the default is true. encoder : Format the recorded event. prudent : If it is true, the log will be safely written to the file, even if other FileAppenders are also writing to this file, the efficiency is low, the default is false.
RollingFileAppender file : The name of the file to be written, which can be a relative directory or an absolute directory. If the parent directory does not exist, it will be created automatically. There is no default value. rollingPolicy : When rolling occurs, it determines the behavior of the RollingFileAppender, involving file movement and reconfiguration. name. The attribute class defines a specific rolling strategy, class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" : The most commonly used rolling strategy, it formulates a rolling strategy based on time, and is responsible for both rolling and starting rolling. There are the following child nodes: fileNamePattern , maxHistory , maxFileSize

rollingPolicy

rollingPolicy :rollingPolicy 类型

Name description
SizeAndTimeBasedRollingPolicy View the size of the currently active file, if it exceeds the specified size, the RollingFileAppender will be notified to trigger the current active file to scroll
TimeBasedRollingPolicy It develops rolling strategies based on time, and is responsible for rolling and starting rolling
triggeringPolicy Inform RollingFileAppender to activate scrolling properly

The complete LOGBACK.XML file

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="1800 seconds"
               debug="false">
    <!-- 定义日志文件 输入位置 -->
    <property name="log_dir" value="log-server" />
    <property name="maxHistory" value="30" />
    <timestamp key="byDay" datePattern="yyyy-MM-dd" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <!-- ERROR级别日志 -->
    <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 RollingFileAppender-->
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录WARN级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>



    <!-- WARN级别日志 appender -->
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录WARN级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/warn.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>




    <!-- INFO级别日志 appender -->
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录INFO级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>

    <!-- DEBUG级别日志 appender -->
    <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录DEBUG级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>

    <!-- TRACE级别日志 appender -->
    <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤器,只记录ERROR级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>TRACE</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/trace.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>

    <appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/all.%i.log
            </fileNamePattern>
            <!-- 日志最大的历史 6天 -->
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} %-5level %logger{36} - %msg%n</pattern>
            <!--%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n-->
        </encoder>
    </appender>
    
    <!-- root级别   DEBUG -->
    <root level="INFO">
        <!-- 控制台输出 -->
        <appender-ref ref="STDOUT" />
        <!-- 文件输出 -->
        <appender-ref ref="ERROR" />
        <appender-ref ref="INFO" />
        <appender-ref ref="WARN" />
        <appender-ref ref="DEBUG" />
        <appender-ref ref="TRACE" />
        <appender-ref ref="ALL" />
    </root>
</configuration>

Guess you like

Origin blog.csdn.net/m0_37111373/article/details/109236867