当前位置:网站首页>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();
边栏推荐
- Mutual exclusion and synchronization of threads
- 附加:token;(没写完,别看…)
- How to apply for company email when registering in company email format?
- JSON data transfer parameters
- 国外的论文在那找?
- Chapter 3 of getting started with MySQL: database creation and operation
- zhvoice
- Go自定义排序
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- Is the multitasking loss in pytoch added up or backward separately?
猜你喜欢
sysdig分析容器系统调用
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
35页危化品安全管理平台解决方案2022版
Highly available cluster (HAC)
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
Xcode real machine debugging
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
Realization of mask recognition based on OpenCV
JSON data transfer parameters
Digital collection trading website domestic digital collection trading platform
随机推荐
How much do you know about synchronized?
MySQL基础
cocospods 的使用
Go自定义排序
yolov5train. py
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Program analysis and Optimization - 9 appendix XLA buffer assignment
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Mutual exclusion and synchronization of threads
How to write the design scheme of the thesis?
[Verilog tutorial]
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
有哪些比较推荐的论文翻译软件?
返回二叉树中最大的二叉搜索子树的根节点
Digital twin visualization solution digital twin visualization 3D platform
How to set automatic reply for mailbox and enterprise mailbox?
Realization of mask recognition based on OpenCV
Which software can translate an English paper in its entirety?
Additional: token; (don't read until you finish writing...)
MySQL Foundation