springboot logback 相关使用
参考:
https://www.cnblogs.com/EasonJim/p/9159195.html
https://blog.csdn.net/tianyaleixiaowu/article/details/73327752
功能:
- logback使用yml配置
- 配置日志地址
- 将日志推送到远程日志系统
示例:
application.yml:
appid: 008
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!-- 说明: 1. 文件的命名和加载顺序有关 logback.xml早于application.yml加载,logback-spring.xml晚于application.yml加载 如果logback配置需要使用application.yml中的属性,需要命名为logback-spring.xml 2. logback使用application.yml中的属性 使用springProperty才可使用application.yml中的值 可以设置默认值 --> <springProperty scope="context" name="appid" source="appid" defaultValue="007"/> <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--> <property name="LOG_HOME" value="/home/work/logs/helpdisk"/> <!-- 彩色日志 --> <!-- 彩色日志依赖的渲染类 --> <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}}"/> <!-- 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 按照每天生成日志文件 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/all.${appid}.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/otherdays/all.%d{yyyyMMddHH}.log</fileNamePattern> <maxHistory>365</maxHistory> </rollingPolicy> </appender> <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/error.${appid}.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} {%thread} %-5level %logger{50} - %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/otherdays/error.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> </appender> <!--打印Http请求和响应参数--> <appender name="HTTP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/http.${appid}.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{50} - %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/otherdays/http.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> </appender> <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender"> <syslogHost>${ip}</syslogHost> <port>${port}</port> <facility>LOCAL1</facility> <suffixPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg</suffixPattern> </appender> <root level="INFO">
<appender-ref ref="SYSLOG"/> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> <appender-ref ref="error"/> </root> </configuration>
配置2
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 说明: 1. 文件的命名和加载顺序有关 logback.xml早于application.yml加载,logback-spring.xml晚于application.yml加载 如果logback配置需要使用application.yml中的属性,需要命名为logback-spring.xml 2. logback使用application.yml中的属性 使用springProperty才可使用application.yml中的值 可以设置默认值 --> <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--> <property name="LOG_HOME" value="/data/logs/kapa-cloud-user-api"/> <!-- 彩色日志 --> <!-- 彩色日志依赖的渲染类 --> <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} [%X{traceId}] %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>--> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}) %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%20.20t]){faint} %clr(requestId:[%X{requestId}]) %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <!-- 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 系统日志输出 --> <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/sys-info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_HOME}/sys-info-%i.log</fileNamePattern> <minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>10MB</maxFileSize> </triggeringPolicy> <!-- 循环政策:基于时间创建日志文件 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志文件名格式 --> <fileNamePattern>${LOG_HOME}/history/info/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 日志最大的历史 60天 --> <maxHistory>15</maxHistory> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/sys-error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_HOME}/sys-error-%i.log</fileNamePattern> <minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>10MB</maxFileSize> </triggeringPolicy> <!-- 循环政策:基于时间创建日志文件 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志文件名格式 --> <fileNamePattern>${LOG_HOME}/history/error/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 日志最大的历史 15 天 --> <maxHistory>15</maxHistory> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤的级别 --> <level>ERROR</level> <!-- 匹配时的操作:接收(记录) --> <onMatch>ACCEPT</onMatch> <!-- 不匹配时的操作:拒绝(不记录) --> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 系统模块日志级别控制 --> <logger name="com.kapa" level="info"/> <!-- 关闭日志 --> <logger name="springfox.documentation.spring.web.scanners.ApiListingReferenceScanner" level="OFF"> </logger> <logger name="springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator" level="OFF"> </logger> <!--系统操作日志--> <root level="info"> <appender-ref ref="STDOUT"/> <appender-ref ref="file_info"/> <appender-ref ref="file_error"/> </root> </configuration>
附录:
logback默认配置
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="log.home" value="D:\\logs" /> <!-- 彩色日志 --> <!-- 彩色日志依赖的渲染类 --> <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}}" /> <!-- Console 输出设置 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <!-- 文件保存日志的相关配置 ERROR --> <appender name="ERROR-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 保存日志文件的路径 --> <!--<file>D:/logs/error/error.log</file>--> <!-- 日志格式 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %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>${log.home}/error/%d{yyyy-MM-dd}-error.log</fileNamePattern> <!-- 最大保存时间:30天--> <maxHistory>30</maxHistory> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <!-- 日志文件最大大小 --> <MaxFileSize>100MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 文件保存日志的相关配置 INFO --> <appender name="INFO-OUT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 保存日志文件的路径 --> <!--<file>D:/logs/info/info.log</file>--> <!-- 日志格式 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %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>${log.home}/info/%d{yyyy-MM-dd}-info.log</fileNamePattern> <!-- 最大保存时间:30天--> <maxHistory>30</maxHistory> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <!-- 日志文件最大大小 --> <MaxFileSize>100MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 输出mybatis的sql语句,没有这句就无法输出mybatis的sql语句,name属性指定到要输出日志的dao,即mybatis要代理的dao --> <logger name="com.cuit.springboot.dao" level="DEBUG" /> <!-- 基于info处理日志:具体控制台或者文件对日志级别的处理还要看所在appender配置的filter,如果没有配置filter,则使用root配置 --> <root level="info"> <appender-ref ref="STDOUT"/> <appender-ref ref="INFO-OUT" /> <appender-ref ref="ERROR-OUT" /> </root> </configuration>