当前位置:网站首页>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();
边栏推荐
- Load balancing cluster (LBC)
- Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
- JDBC教程
- 带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
- yolov5train. py
- How to apply for company email when registering in company email format?
- Codeforces Round #771 (Div. 2)---A-D
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
- zhvoice
- Digital twin smart factory develops digital twin factory solutions
猜你喜欢
【ML】李宏毅三:梯度下降&分类(高斯分布)
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
MySQL基础
95 pages of smart education solutions 2022
Why can't the start method be called repeatedly? But the run method can?
95页智慧教育解决方案2022
Digital collection trading website domestic digital collection trading platform
采用VNC Viewer方式远程连接树莓派
Load balancing cluster (LBC)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
随机推荐
JDBC tutorial
How to apply for company email when registering in company email format?
Convolution和Batch normalization的融合
可知论与熟能生巧
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
Bean加载控制
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
非路由组件之头部组件和底部组件书写
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
Leetcode DP three step problem
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
Pytorch里面多任务Loss是加起来还是分别backward?
Digital twin smart factory develops digital twin factory solutions
【OJ】两个数组的交集(set、哈希映射 ...)
A single element in an ordered array -- Valentine's Day mental problems
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Codeforces Round #771 (Div. 2)---A-D
Digital twin visualization solution digital twin visualization 3D platform
RTP 接发ps流工具改进(二)
采用VNC Viewer方式遠程連接樹莓派