当前位置:网站首页>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();
边栏推荐
- Agnosticism and practice makes perfect
- Dishes launcher small green program and directory management (efficiency tool)
- A single element in an ordered array -- Valentine's Day mental problems
- [Verilog tutorial]
- Difference between NVIDIA n card and amda card
- Container runtime analysis
- Digital twin smart factory develops digital twin factory solutions
- Sourcetree details
- Chapter 4 of getting started with MySQL: data types stored in data tables
- 经济学外文文献在哪查?
猜你喜欢
JDBC practice cases
Xcode real machine debugging
JDBC練習案例
Architecture: database architecture design
请问大家在什么网站上能查到英文文献?
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
How to apply for company email when registering in company email format?
Load balancing cluster (LBC)
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
随机推荐
S12. Verify multi host SSH mutual access script based on key
基于OpenCV实现口罩识别
教育学大佬是怎么找外文参考文献的?
35页危化品安全管理平台解决方案2022版
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Digital twin visualization solution digital twin visualization 3D platform
List of major chip Enterprises
Xcode real machine debugging
返回二叉树中最大的二叉搜索子树的大小
Where can I check the foreign literature of economics?
1380. Lucky numbers in the matrix
JDBC教程
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
RTP 接发ps流工具改进(二)
67页新型智慧城市整体规划建设方案(附下载)
How to apply for company email when registering in company email format?
Many to one, one to many processing
Bean加载控制
Architecture: load balancing
zhvoice