当前位置:网站首页>Slf4j + logback logging framework
Slf4j + logback logging framework
2022-07-03 00:13:00 【sword to coding】
First introduce related dependencies ( Of course, many packages will indirectly rely on these dependencies )
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
Use here Slf4j Why , There are many log frameworks on the market ,Slf4j It can be regarded as an interface or similar jdbc, Integrate different logging frameworks , It is convenient for us to change the logging framework in a project , It can be well maintained . Here we are using Slf4j + logback The way .
in addition ,Slf4j Placeholders are used for log output {} The way , Compared with other logging frameworks, it can avoid many performance problems caused by string splicing .
Next, let's talk about logback The problem with the configuration file :
<?xml version="1.0" encoding="UTF-8"?>
<!-- scan:-->
<!-- When this property is set to true when , If the configuration file changes , Will be reloaded , The default value is true.-->
<!-- scanPeriod:-->
<!-- Set the time interval between changes in the monitoring profile , If no time unit is given , The default unit is milliseconds . When scan by true when , This property takes effect . The default time interval is 1 minute .-->
<!-- debug:-->
<!-- When this property is set to true when , Will print out logback Internal log information , Real-time view logback Running state . The default value is false.-->
<configuration scan="true" scanPeriod="10 seconds">
<contextName>austin</contextName>
<!-- Set log output path You can make “${}” To use variables .TODO The configuration can be read later -->
<property name="log.path" value="logs"/>
<springProperty scope="context" name="grayLogIp" source="austin.business.graylog.ip"/>
<!-- In a configuration There can be more than one appender appender Mainly specify the output destination Which attribute name Used to specify self-defined names class Used to specify the destination of the output -->
<!-- ch.qos.logback.core.ConsoleAppender The output of the console -->
<!-- ch.qos.logback.core.rolling.RollingFileAppender File scrolling output : Now write the log to a file , Under certain conditions , Output some logs to another file -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!-- Format output :%d Indicates the date ,%thread Represents the thread name ,%-5level: The level is shown from the left 5 Character width %msg: Log message ,%n Is a newline -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<!-- Set character set -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- file The file path of the log output , It can be absolute path or relative path -->
<!-- encoder Formatting method -->
<!-- rollingPolicy Rolling strategy through class To specify the scrolling strategy -->
<!-- The most common scrolling strategy is Time based rolling strategy ch.qos.logback.core.rolling.TimeBasedRollingPolicy-->
<!-- fileNamePattern Used to refer to the file name format of the produced rolling log -->
<!-- Time scrolling means that the generated log will not only be printed in one file , It will be printed into different files with a certain order according to a certain strategy -->
<!-- Time scrolling output level by INFO journal -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- The path and filename of the log file being logged -->
<file>${log.path}/austin-info.log</file>
<!-- Log file output format -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- Rolling policy for loggers , By date , Record by size -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily log archive path and format -->
<fileNamePattern>${log.path}/logs/austin-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- When more than 1000MB when , Trigger scrolling strategy -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1000MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Log file retention days -->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- This log file only records info Grade -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- Time scrolling output level by ERROR journal -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- The path and filename of the log file being logged -->
<file>${log.path}/austin-error.log</file>
<!-- Log file output format -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- Set character set here -->
</encoder>
<!-- Rolling policy for loggers , By date , Record by size -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logs/austin-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1000MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Log file retention days -->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- This log file only records ERROR Grade -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
<!-- Graylog Address of service -->
<graylogHost>${grayLogIp}</graylogHost>
<!-- UDP Input port -->
<graylogPort>12201</graylogPort>
<!-- Maximum GELF Block size ( Company : byte ),508 Is the recommended minimum , The maximum value is 65467 -->
<maxChunkSize>508</maxChunkSize>
<!-- Use compression -->
<useCompression>true</useCompression>
<encoder class="de.siegmar.logbackgelf.GelfEncoder">
<!-- Whether to send native log information -->
<includeRawMessage>false</includeRawMessage>
<includeMarker>true</includeMarker>
<includeMdcData>true</includeMdcData>
<includeCallerData>false</includeCallerData>
<includeRootCauseData>false</includeRootCauseData>
<!-- Whether to send the name of the log level , Otherwise, the log level is represented by a number by default -->
<includeLevelName>true</includeLevelName>
<shortPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%m%nopex</pattern>
</shortPatternLayout>
<fullPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d - [%thread] %-5level %logger{35} - %msg%n</pattern>
</fullPatternLayout>
<!-- Configuration application name ( The service name ), adopt staticField The tag can customize some fixed log fields -->
<staticField>app_name:austin</staticField>
</encoder>
</appender>
<root level="info">
<!-- TODO console You can print only for dev Environmental -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="ERROR_FILE"/>
<appender-ref ref="GELF"/>
</root>
</configuration>
When spring When loading, it will automatically go to the path to find logback.xml The name of the file and load the configuration
Use
@Slf4j
class UserServiceImpl{
public void deleteUser(int id){
log.info(" Pass in id by {}",id);
}
}
@Slf4j yes Lombok Annotations , Instead of the sentence
Logger log=LoggerFactory.getLogger();
边栏推荐
猜你喜欢

67页新型智慧城市整体规划建设方案(附下载)

MySQL Foundation

Architecture: database architecture design

Xcode real machine debugging

程序分析与优化 - 9 附录 XLA的缓冲区指派

JDBC練習案例

秒杀系统设计

来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能

Digital twin visualization solution digital twin visualization 3D platform

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
随机推荐
Develop knowledge points
1380. Lucky numbers in the matrix
Realization of mask recognition based on OpenCV
请问大家在什么网站上能查到英文文献?
JDBC練習案例
Speech recognition Series 1: speech recognition overview
监控容器运行时工具Falco
Codeforces Round #771 (Div. 2)---A-D
How QT exports data to PDF files (qpdfwriter User Guide)
35 pages dangerous chemicals safety management platform solution 2022 Edition
实用系列丨免费可商用视频素材库
程序分析与优化 - 9 附录 XLA的缓冲区指派
【OJ】两个数组的交集(set、哈希映射 ...)
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Leetcode relaxation question - day of the week
Agnosticism and practice makes perfect
英文论文有具体的格式吗?
How much do you know about synchronized?
返回二叉树中最大的二叉搜索子树的大小