当前位置:网站首页>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 page overall planning and construction plan for a new smart city (download attached)
- MATLAB signal processing [Q & a notes-1]
- 哪些软件可以整篇翻译英文论文?
- sourcetree 详细
- RTP 接发ps流工具改进(二)
- 来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
- Maybe you read a fake Tianlong eight
- JDBC Exercise case
- Request and response
- [shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
猜你喜欢

Program analysis and Optimization - 9 appendix XLA buffer assignment

流媒体技术优化

How QT exports data to PDF files (qpdfwriter User Guide)

JDBC練習案例

MySQL advanced learning notes (III)

接口差异测试——Diffy工具

JDBC tutorial

带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)

Dishes launcher small green program and directory management (efficiency tool)

秒杀系统设计
随机推荐
实用系列丨免费可商用视频素材库
Question e: merged fruit -noip2004tgt2
Missing number
How to apply for company email when registering in company email format?
写论文可以去哪些网站搜索参考文献?
Realization of mask recognition based on OpenCV
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
TypeError: Cannot read properties of undefined (reading ***)
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
A single element in an ordered array -- Valentine's Day mental problems
请问大家在什么网站上能查到英文文献?
How to write the design scheme of the thesis?
返回二叉树两个节点间的最大距离
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
Optimization of streaming media technology
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
哪些软件可以整篇翻译英文论文?
Speech recognition Series 1: speech recognition overview
67 page overall planning and construction plan for a new smart city (download attached)