当前位置:网站首页>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();
边栏推荐
猜你喜欢

Improvement of RTP receiving and sending PS stream tool (II)

What are the projects of metauniverse and what are the companies of metauniverse
![洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举](/img/a3/55bb71d39801ceeee421a0c8ded333.png)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举

How to apply for company email when registering in company email format?
![[Verilog tutorial]](/img/15/d5e188a15e22fa44f1756fc492099d.jpg)
[Verilog tutorial]

直击产业落地!飞桨重磅推出业界首个模型选型工具

Angled detection frame | calibrated depth feature for target detection (with implementation source code)

Program analysis and Optimization - 9 appendix XLA buffer assignment

Realization of mask recognition based on OpenCV

CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
随机推荐
Bean load control
国外的论文在那找?
大学生课堂作业2000~3000字的小论文,标准格式是什么?
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
Interface automation coverage statistics - used by Jacobo
maya渔屋建模
JDBC练习案例
leetcode 650. 2 keys keyboard with only two keys (medium)
TypeError: Cannot read properties of undefined (reading ***)
PR FAQ, what about PR preview video card?
Where can I check the foreign literature of economics?
How to set automatic reply for mailbox and enterprise mailbox?
哪些软件可以整篇翻译英文论文?
Digital twin smart factory develops digital twin factory solutions
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
[reading notes] phased summary of writing reading notes
MySQL advanced learning notes (III)
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Wechat applet basic learning (wxss)
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office