当前位置:网站首页>Slf4j + Logback日志框架
Slf4j + Logback日志框架
2022-07-02 22:53:00 【sword to coding】
先引入相关依赖(当然很多包会间接依赖这些依赖)
<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>
这里使用Slf4j的原因,市面上有很多日志框架,Slf4j可以看作一个接口或类似jdbc,将不同的日志框架整合起来,方便我们在一个项目中需要改变日志框架时,可以很好的进行维护。这里我们使用的是Slf4j + logback的方式。
另外,Slf4j 日志输出采用占位符{}的方式,相对于其他的日志框架可以避免很多字符串拼接而造成的性能问题。
接下来聊一聊关于logback配置文件的问题:
<?xml version="1.0" encoding="UTF-8"?>
<!-- scan:-->
<!-- 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。-->
<!-- scanPeriod:-->
<!-- 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。-->
<!-- debug:-->
<!-- 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。-->
<configuration scan="true" scanPeriod="10 seconds">
<contextName>austin</contextName>
<!-- 设置日志输出路径 可以使“${}”来使用变量。TODO 后面是可以读配置的 -->
<property name="log.path" value="logs"/>
<springProperty scope="context" name="grayLogIp" source="austin.business.graylog.ip"/>
<!-- 在一个configuration下可以有多个appender appender主要指定输出目的地 其中属性name用于指定自己定义的名字 class用于指定输出的目的地-->
<!-- ch.qos.logback.core.ConsoleAppender 输出的控制台-->
<!-- ch.qos.logback.core.rolling.RollingFileAppender 文件滚动输出:现将日志写到一个文件中,在特定的条件下,将某些日志输出到另一个文件-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<!-- 设置字符集 -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- file日志输出的文件路径,可以是绝对路径也可以是相对路径-->
<!-- encoder格式化方式-->
<!-- rollingPolicy滚动策略 也是通过class来指定滚动策略-->
<!-- 最常见的滚动策略为 基于时间的滚动策略 ch.qos.logback.core.rolling.TimeBasedRollingPolicy-->
<!-- fileNamePattern用于指生产出来的滚动日志的文件名称格式-->
<!--时间滚动也即是生成的日志不仅仅会只打印在一个文件中,会根据一定的策略打印到不同的有一定顺序的文件中-->
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/austin-info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${log.path}/logs/austin-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- 当超过1000MB时,触发滚动策略-->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1000MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/austin-error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<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>
<!--日志文件保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<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服务的地址 -->
<graylogHost>${grayLogIp}</graylogHost>
<!-- UDP Input端口 -->
<graylogPort>12201</graylogPort>
<!-- 最大GELF数据块大小(单位:字节),508为建议最小值,最大值为65467 -->
<maxChunkSize>508</maxChunkSize>
<!-- 是否使用压缩 -->
<useCompression>true</useCompression>
<encoder class="de.siegmar.logbackgelf.GelfEncoder">
<!-- 是否发送原生的日志信息 -->
<includeRawMessage>false</includeRawMessage>
<includeMarker>true</includeMarker>
<includeMdcData>true</includeMdcData>
<includeCallerData>false</includeCallerData>
<includeRootCauseData>false</includeRootCauseData>
<!-- 是否发送日志级别的名称,否则默认以数字代表日志级别 -->
<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>
<!-- 配置应用名称(服务名称),通过staticField标签可以自定义一些固定的日志字段 -->
<staticField>app_name:austin</staticField>
</encoder>
</appender>
<root level="info">
<!-- TODO console打印后面可以只针对dev环境的 -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="ERROR_FILE"/>
<appender-ref ref="GELF"/>
</root>
</configuration>
当spring加载时会自动去路径下找到logback.xml的名字的文件并加载配置
使用
@Slf4j
class UserServiceImpl{
public void deleteUser(int id){
log.info("传入id为{}",id);
}
}
@Slf4j是Lombok的注解,代替了一下语句
Logger log=LoggerFactory.getLogger();
边栏推荐
- Bean load control
- Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
- 基于OpenCV实现口罩识别
- 附加:token;(没写完,别看…)
- PR FAQ, what about PR preview video card?
- zhvoice
- Convolution和Batch normalization的融合
- Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
- Digital twin smart factory develops digital twin factory solutions
- Container runtime analysis
猜你喜欢
Use redis to realize self increment serial number
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Matlab 信号处理【问答笔记-1】
Convolution和Batch normalization的融合
What are the projects of metauniverse and what are the companies of metauniverse
JDBC练习案例
How much do you know about synchronized?
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
How to apply for company email when registering in company email format?
随机推荐
直击产业落地!飞桨重磅推出业界首个模型选型工具
Optimization of streaming media technology
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
leetcode 650. 2 keys keyboard with only two keys (medium)
【OJ】两个数组的交集(set、哈希映射 ...)
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
Matlab 信号处理【问答笔记-1】
Codeforces Round #771 (Div. 2)---A-D
JDBC練習案例
Agnosticism and practice makes perfect
Top Devops tool chain inventory
程序分析与优化 - 9 附录 XLA的缓冲区指派
Digital twin visualization solution digital twin visualization 3D platform
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能