springboot中配置logback+slf4j
程序员文章站
2022-04-30 21:10:19
...
1.引入依赖
Spring Boot 默认的日志框架 Logback+SLF4J,不需要在引入依赖
2.配置文件logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan = "true">
<!-- 日志输出目录,前缀:/logs/order/【***】 后面填写项目名称 -->
<property name="LOG_HOME" value="/usr/logs/ibp/sm" />
<property name="PATTERN_LAYOUT"
value="%d %-5level %logger %line - %msg%n" />
<!-- 最大保存天数-->
<property name="maxHistory" value="15"/>
<!-- 彩色日志 控制台 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--普通日志格式-->
<property name="NORMAL_PATTERN" value="%date [%level] [%thread] %logger{60} [%file : %line] %msg%n" />
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- 日志输出编码 -->
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="file-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/debug.log</file>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志归档-->
<FileNamePattern>${LOG_HOME}/debug-%d{yyyy-MM-dd}%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数 -->
<MaxHistory>${maxHistory}</MaxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${PATTERN_LAYOUT}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>${maxHistory}</MaxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${PATTERN_LAYOUT}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<appender name="file-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>${maxHistory}</MaxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${PATTERN_LAYOUT}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<appender name="async-file" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1000</queueSize>
<appender-ref ref="file-debug" />
</appender>
<!-- 打印sql的日志,name的值为 dao包所在的路径-->
<logger name="com.coship" level="DEBUG" />
<logger name="dao" level="DEBUG" />
<root level="INFO">
<appender-ref ref="stdout"/>
<!-- 开发阶段可以屏蔽,生产环境打印输出到文件-->
<appender-ref ref="file-debug" />
<appender-ref ref="file-info" />
<appender-ref ref="file-error" />
</root>
</configuration>