完整的日志文件 https://www.cnblogs.com/gyjx2016/p/5900035.html
日志拆分的原因:
1、抽取公共部分统一管理
2、统一日志格式
3、方便维护
1、新建base-logback.xml
<!--定义公共配置--> <included> <!--日志公共配置--> <!--日志格式--> <property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %X{TRACE_ID} [%thread] %-5level %logger[%L] - %m%n"/> <!--日志编码--> <property name="CHARSET" value="utf-8"/> <!--单个日志文件大小--> <property name="MAX_FILE_SIZE" value="10Kb"/> <!--日志过期时间--> <property name="MAX_HISTORY" value="1"/> <!--日志根路径--> <property name="BASE_LOG_PATH" value="/data/logs"/> <!--控制台打印日志--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${PATTERN}</pattern> <charset>${CHARSET}</charset> </encoder> </appender> <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--当前日志文件名--> <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.log</File> <!--日志级别过滤--> <filter class="com.luna.log.filter.LogbackInfoAndDebugFilter"></filter> <!--日志滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志归档文件名--> <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.log.%d{yyyy-MM-dd}.log.%i.gz </fileNamePattern> <!--按天滚动--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!--单个日志文件最大,也会滚动--> <maxFileSize>${MAX_FILE_SIZE}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!--日志存活时间,大于这个时间的日志都将会删除--> <maxHistory>${MAX_HISTORY}</maxHistory> </rollingPolicy> <encoder> <!--日志格式--> <pattern>${PATTERN}</pattern> <!--日志编码--> <charset>${CHARSET}</charset> </encoder> </appender> <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.log</File> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.log.%d{yyyy-MM-dd}.log.%i.gz </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${MAX_FILE_SIZE}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>${MAX_HISTORY}</maxHistory> </rollingPolicy> <encoder> <pattern>${PATTERN}</pattern> <charset>${CHARSET}</charset> </encoder> </appender> <!--开启logback异步记录日志--> <appender name="async-console" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="console"/> <includeCallerData>true</includeCallerData> </appender> <appender name="async-fileAppender" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="fileAppender"/> <includeCallerData>true</includeCallerData> </appender> <appender name="async-errorAppender" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="errorAppender"/> <includeCallerData>true</includeCallerData> </appender> <!--整个项目打印INFO 以上级别--> <logger name="com.luna" level="INFO"> <appender-ref ref="async-fileAppender"/> <appender-ref ref="async-errorAppender"/> </logger> <!--记录druid-sql的记录--> <logger name="druid.sql.Statement" level="debug" additivity="false"> <appender-ref ref="async-console"/> <appender-ref ref="async-fileAppender"/> </logger> </included>
2、新建logback-spring.xml引用base-logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!--日志服务名--> <property scope="local" name="SERVICE_NAME" value="demo"/> <!--引用公共日志--> <include resource="base-logback.xml"/> <!--全局打印INFO 级别日志--> <root level="INFO"> <appender-ref ref="async-console"/> </root> </configuration>
因为我的项目是springboot构建,所以命名logback-spring.xml,这种命名可以使用spring标签
全文完,感谢您的耐心阅读~
欢迎大家关注我的公众号