- application.properties 配置logback.xml 路径
注:如果logback.xml在默认的 src/main/resources 目录下则不需要配置application.properties路径
logging.config=classpath:log/logback.xml - 配置logback.xml
注:logback.xml放在src/main/resources目录下新建log文件夹里;<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <configuration scan="true"> <property resource="log/logback.properties" /> <!-- 控制台日志配置 --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <!-- 格式化 --> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] [%t] %c{36} - %m%n</pattern> </encoder> </appender> <!-- info级别日志控制 --> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件路径 --> <file>${logback.path}/info.log</file> <!-- 是否追加 默认为true --> <append>true</append> <!-- 滚动策略 日期+大小 策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${logback.zip.path}/%d{yyyy-MM-dd}/info/info-%i.zip</fileNamePattern> <!-- 单个日志大小 --> <maxFileSize>${logback.maxFileSize}</maxFileSize> <!-- 日志保存周期 --> <maxHistory>${logback.maxHistory}</maxHistory> <!-- 总大小 --> <totalSizeCap>${logback.totalSizeCap}</totalSizeCap> </rollingPolicy> <!-- 格式化 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] [%t] %c{36} - %m%n</pattern> </encoder> <!-- 级别过滤 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- warn级别日志控制 --> <appender name="WARN_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件路径 --> <file>${logback.path}/warn.log</file> <!-- 是否追加 默认为true --> <append>true</append> <!-- 滚动策略 日期+大小 策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${logback.zip.path}/%d{yyyy-MM-dd}/warn/warn-%i.zip</fileNamePattern> <!-- 单个日志大小 --> <maxFileSize>${logback.maxFileSize}</maxFileSize> <!-- 日志保存周期 --> <maxHistory>${logback.maxHistory}</maxHistory> <!-- 总大小 --> <totalSizeCap>${logback.totalSizeCap}</totalSizeCap> </rollingPolicy> <!-- 格式化 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] [%t] %c{36} - %m%n</pattern> </encoder> <!-- 级别过滤 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- ERROR级别日志控制 --> <appender name="ERROR_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件路径 --> <file>${logback.path}/error.log</file> <!-- 是否追加 默认为true --> <append>true</append> <!-- 滚动策略 日期+大小 策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${logback.zip.path}/%d{yyyy-MM-dd}/error/error-%i.zip</fileNamePattern> <!-- 单个日志大小 --> <maxFileSize>${logback.maxFileSize}</maxFileSize> <!-- 日志保存周期 --> <maxHistory>${logback.maxHistory}</maxHistory> <!-- 总大小 --> <totalSizeCap>${logback.totalSizeCap}</totalSizeCap> </rollingPolicy> <!-- 格式化 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] [%t] %c{36} - %m%n</pattern> </encoder> <!-- 级别过滤 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <root level="${logback.level}"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE_INFO" /> <appender-ref ref="WARN_INFO" /> <appender-ref ref="ERROR_INFO" /> </root> </configuration>
- 在log目录下配置logback.properties
logback.path=/var/log/project_name logback.zip.path=/var/log/zip/project_name logback.maxFileSize=xMB logback.maxHistory=15 logback.totalSizeCap=xxxMB logback.level=DEBUG
原文地址:https://www.cnblogs.com/aben-blog/p/8968363.html
时间: 2024-11-09 14:10:13