当前位置: 首页>后端>正文

Logback:开源日志组件

Logback:开源日志组件,第1张

slf4j是一系列的日志接口,而log4j、logback是具体实现了的日志框架

log4j:是apache实现的一个开源日志组件。

logback:同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架。是slf4j的原生实现,也就是说logback实现slf4j是不消耗内存和计算开销的。

Logback是SpringBoot内置的日志处理框架,spring-boot-starter其中包含了spring-boot-starter-logging,该依赖内容就是 Spring Boot 默认的日志框架 logback。

Logback相比log4j的优势,

比log4j更快,重写了内核,在一些关键路径上性能提升10倍,内存占用也更少。

经过大量的测试,和log4j测试不在一个量级。

logback-classic是SLF4J的实现,切换其他日志框架非常方便,

文档丰富并且不断更新,支持Groovy风格的配置文件

配置文件自动重新加载,如果更新了配置文件,logback-classic可以自动重新加载。

自动压缩归档的日志文件,压缩文件是异步进行,不影响应用。

配置文件可以处理不同的环境开发,测试,生产,这样一个配置文件就可以适应多个环境。

官方文档:http://logback.qos.ch/manual/

logback有5种日志级别,等级从低到高分别是TRACE < DEBUG < INFO < WARN < ERROR

Trace:是追踪,就是程序推进以下

Debug:指出细粒度信息事件对调试应用程序是非常有帮助的.

Info:消息在粗粒度级别上突出强调应用程序的运行过程.

Warn:输出警告及warn以下级别的日志.

Error:输出错误信息日志.

OFF表示关闭全部日志

ALL表示开启全部日志。

@Slf4j

Log.debug() / log.info()/ log.warn() / log.error()

<?xml version="1.0" encoding="UTF-8"?>

<configuration

????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

????????xsi:noNamespaceSchemaLocation="http://www.padual.com/java/logback.xsd"

????????debug="false" scan="true" scanPeriod="30 second">

????<property name="PROJECT" value="iorder" />

????<property name="ROOT" value="logs/${PROJECT}/" />

????<property name="FILESIZE" value="50MB" />

????<property name="MAXHISTORY" value="100" />

????<timestamp key="DATETIME" datePattern="yyyy-MM-dd HH:mm:ss" />

????<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????</appender>

????<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????????<filter class="ch.qos.logback.classic.filter.LevelFilter">

????????????<level>ERROR</level>

????????????<onMatch>ACCEPT</onMatch>

????????????<onMismatch>DENY</onMismatch>

????????</filter>

????????<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

????????????<fileNamePattern>${ROOT}%d/error.%i.log</fileNamePattern>

????????????<maxHistory>${MAXHISTORY}</maxHistory>

????????????<timeBasedFileNamingAndTriggeringPolicy

????????????????????class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

????????????????<maxFileSize>${FILESIZE}</maxFileSize>

????????????</timeBasedFileNamingAndTriggeringPolicy>

????????</rollingPolicy>

????</appender>

????<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????????<filter class="ch.qos.logback.classic.filter.LevelFilter">

????????????<level>WARN</level>

????????????<onMatch>ACCEPT</onMatch>

????????????<onMismatch>DENY</onMismatch>

????????</filter>

????????<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

????????????<fileNamePattern>${ROOT}%d/warn.%i.log</fileNamePattern>

????????????<maxHistory>${MAXHISTORY}</maxHistory>

????????????<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

????????????????<maxFileSize>${FILESIZE}</maxFileSize>

????????????</timeBasedFileNamingAndTriggeringPolicy>

????????</rollingPolicy>

????</appender>

????<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????????<filter class="ch.qos.logback.classic.filter.LevelFilter">

????????????<level>INFO</level>

????????????<onMatch>ACCEPT</onMatch>

????????????<onMismatch>DENY</onMismatch>

????????</filter>

????????<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

????????????<fileNamePattern>${ROOT}%d/info.%i.log</fileNamePattern>

????????????<maxHistory>${MAXHISTORY}</maxHistory>

????????????<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

????????????????<maxFileSize>${FILESIZE}</maxFileSize>

????????????</timeBasedFileNamingAndTriggeringPolicy>

????????</rollingPolicy>

????</appender>

????<appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????????<filter class="ch.qos.logback.classic.filter.LevelFilter">

????????????<level>DEBUG</level>

????????????<onMatch>ACCEPT</onMatch>

????????????<onMismatch>DENY</onMismatch>

????????</filter>

????????<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

????????????<fileNamePattern>${ROOT}%d/debug.%i.log</fileNamePattern>

????????????<maxHistory>${MAXHISTORY}</maxHistory>

????????????<timeBasedFileNamingAndTriggeringPolicy

????????????????????class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

????????????????<maxFileSize>${FILESIZE}</maxFileSize>

????????????</timeBasedFileNamingAndTriggeringPolicy>

????????</rollingPolicy>

????</appender>

????<appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">

????????<encoder charset="utf-8">

????????????<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n

????????????</pattern>

????????</encoder>

????????<filter class="ch.qos.logback.classic.filter.LevelFilter">

????????????<level>TRACE</level>

????????????<onMatch>ACCEPT</onMatch>

????????????<onMismatch>DENY</onMismatch>

????????</filter>

????????<rollingPolicy

????????????????class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

????????????<fileNamePattern>${ROOT}%d/trace.%i.log</fileNamePattern>

????????????<maxHistory>${MAXHISTORY}</maxHistory>

????????????<timeBasedFileNamingAndTriggeringPolicy

????????????????????class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

????????????????<maxFileSize>${FILESIZE}</maxFileSize>

????????????</timeBasedFileNamingAndTriggeringPolicy>

????????</rollingPolicy>

????</appender>

????<logger name="org.apache.ibatis" level="INFO" additivity="false" />

????<logger name="org.mybatis.spring" level="INFO" additivity="false" />

????<logger name="com.github.miemiedev.mybatis.paginator" level="INFO" additivity="false" />

????<root level="DEBUG">

????????<appender-ref ref="STDOUT" />

????????<appender-ref ref="DEBUG" />

????????<appender-ref ref="ERROR" />

????????<appender-ref ref="WARN" />

????????<appender-ref ref="INFO" />

????????<appender-ref ref="TRACE" />

????</root>

</configuration>


https://www.xamrdz.com/backend/3e81929001.html

相关文章: