Super detailed explanation of logback log entry

Basic Information

Log : It is the ability to accurately and accurately describe what happens in the running state of the system (connection timeout, user operation, exception throwing, etc.);
Log framework : It is an integrated toolkit that can uniformly standardize log information and output.

Logback advantages

What are the advantages of the Logback framework, first of all, we have to understand what the framework can help me to do?

  1. Custom output path, easy maintenance later (convenient for operation and maintenance partners)

    能够将日志信息存储到本地文件或存储到数据库中去,而且根据滚动策略设置一天一个文件
    
  2. Customize the output format of the log according to your preferences

    可以通过配置文件修改日志文件的具体内容,在不改变原有业务代码的情况下改变输出格式,方便调试
    
  3. Carry log context information with you

    上下文中包含着时间戳、类的所在路径、所占用的堆栈信息和占用的线程等等
    
  4. You can choose what to output at runtime

     运行无误的时候,我们只在乎返回的运行信息;运行出错的时候,选择返回错误信息
    
  5. Convenient configuration and high performance

     框架与业务代码耦合小,正常业务之外的审计和运维需求,与系统性能无关
    

Frame configuration

There are two common configuration schemes for logback:

  1. Configure in the application.properties/application.yml that comes with the SpringBoot project (this configuration is simple configuration, and SpringBoot has been integrated).
    This is generally suitable for personal learning projects, and can be recommended for rapid development
    application
  2. Create a logback.xml configuration file separately under the resources file for more complex configuration. (Recommended, the configuration information is sufficiently detailed)

Overall code

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="300 seconds" debug="false">
	<!-- 定义参数参数  -->
	<!-- TRACE<DEBUG<INFO<WRAN<ERROR -->
	<property name="log.level" value="debug"/>
	<property name="log.maxHistory" value="30"/>
	<property name="log.filePath" value="E:/study"/>
	<property name="log.pattern"
	 value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n"/>
	<!-- 控制台设置 -->
	<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
	</appender>
	<!-- DEBUG -->
	<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/debug.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留debug优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	
	<!-- INFO -->
	<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/info.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留info优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	
	<!-- ERROR -->
	<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/webapps/error.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留error优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	
		<!-- 该 logger 只会记录debug 优先级别以上的日志信息 -->
		<!-- 子logger -->
 	<logger name="com.hyxiao.logback.test" level="${log.level}" additivity="true">
 		<!-- 与append相绑定,logger文件会往这三个对象输出日志信息 -->
		<appender-ref ref="debugAppender"/>
		<appender-ref ref="infoAppender"/>
		<appender-ref ref="errorAppender"/>
	</logger>
	<!-- 父logger(上面的logger为子logger),当根logger没有设置 level 时 -->
	<!-- 则会继承父logger设置的lever等级,即输出 info(warn、error) 级别的信息 -->
	<root level="info">
		<appender-ref ref="consoleAppender"/>
	</root>
</configuration>

Code analysis

  • The first is the configuration parent tag. In general, this tag does not need to add any attributes (optional)
<configuration scan="true" scanPeriod="300 seconds" debug="false">

Set scan = "true", the configuration file will be reloaded if it changes, the default is true.
Set scanPeriod="300 seconds", after opening scan, it will scan for changes in configuration files within 300s.
Set debug = "false", when set to false, the system will not output the log information at the bottom of logback, that is, the running status of logback

  • Followed by the property sub-tag, set logback parameters (optional)
	<property name="log.level" value="debug"/>
	<property name="log.maxHistory" value="30"/>
	<property name="log.filePath" value="E:/study"/>
	<property name="log.pattern"
	 value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n"/>

log.level refers to the log level of logback. The debug
log level is divided into five levels, namely TRACE <DEBUG <INFO <WRAN <ERROR.
When the level is set to debug, the output will be output according to the priority, and debug and info will be output in turn. For the four levels of information, wran, and error, the trace priority is not enough.
The priority diagram of logback is as follows:
priority
log.maxHistory refers to the maximum number of files saved in history
log.filePath refers to the root path of file storage
log.pattern refers to the final format of the log presentation,

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n

%d: refers to the time format, {year, month, day, hour, minute, second}
[%thread]: which thread belongs to execute
%-5level: the level is displayed from the left with a width of 5 characters, indented by 5 digits to display log level information
%logger {50} -%msg: log information output in which class
%n: line break

  • Next is the appender subtag

Encoder property (${log.pattern} has been configured in the property tag and can be quoted directly)

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
			<pattern>${log.pattern}</pattern>
        </layout>
    </appender>

DEBUG level setting

	<!-- DEBUG -->
	<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/debug.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留debug优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>

INFO level settings

	<!-- INFO -->
	<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/info.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留info优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>

ERROR level setting

	<!-- ERROR -->
	<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径 -->
		<file>${log.filePath}/logs/webapps/error.log</file>
		<!-- 滚动策略 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>
				${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<!-- 过滤器(仅保留error优先级的日志信息) -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
  • The last sub-tags are logger and root, which are used to store log objects and inform logback of the class information under which package to pay attention to
		<!-- 该 logger 只会记录debug 优先级别以上的日志信息 -->
		<!-- 子logger -->
 	<logger name="com.hyxiao.logback.test" level="${log.level}" additivity="true">
 		<!-- 与append相绑定,logger文件会往这三个对象输出日志信息 -->
		<appender-ref ref="debugAppender"/>
		<appender-ref ref="infoAppender"/>
		<appender-ref ref="errorAppender"/>
	</logger>
	<!-- 父logger(上面的logger为子logger),当根logger没有设置 level 时 -->
	<!-- 则会继承父logger设置的lever等级,即输出 info(warn、error) 级别的信息 -->
	<root level="info">
		<appender-ref ref="consoleAppender"/>
	</root>

The "additivity" attribute above is set to true by default. When set to true, the child logger will pass the information it collects to the root. Here, the child logger will go to the root tag and add appender-ref to the child logger(). , That is, the child logger can also support outputting log information on the console.

review

logger : Mainly used to store log objects and define log types and levels.
appender : Used to specify the destination of the log output, the destination can be a console, a file, and so on.
layout : Format the output information of the log.

~~~~ ~~~~ ~~~~

It is not easy to make, if it helps, please give me a like, thank you! ! !

Guess you like

Origin blog.csdn.net/hyx1249273846/article/details/113632780