当前位置:网站首页>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();
边栏推荐
- Agnosticism and practice makes perfect
- 【ML】李宏毅三:梯度下降&分类(高斯分布)
- 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
- Interface switching based on pyqt5 toolbar button -1
- 流媒体技术优化
- MySQL Foundation
- How much do you know about synchronized?
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
猜你喜欢

How does win11 turn on visual control? Win11 method of turning on visual control

基于OpenCV实现口罩识别
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth

JDBC练习案例

How much do you know about synchronized?

Digital collection trading website domestic digital collection trading platform

JSON data transfer parameters

QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)

JDBC tutorial
随机推荐
67 page overall planning and construction plan for a new smart city (download attached)
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
容器运行时分析
Returns the size of the largest binary search subtree in a binary tree
JDBC练习案例
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
S12. Verify multi host SSH mutual access script based on key
PR FAQ, what about PR preview video card?
流媒体技术优化
leetcode 650. 2 keys keyboard with only two keys (medium)
Writing of head and bottom components of non routing components
JVM foundation review
判断二叉树是否为满二叉树
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
Interface automation coverage statistics - used by Jacobo
Optimization of streaming media technology
[array] binary search
Pytorch里面多任务Loss是加起来还是分别backward?