1.导入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
2.创建logback-spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- scan 配置文件如果发生改变,将会被重新加载 scanPeriod 检测间隔时间--> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <contextName>zlyPay-log</contextName> <!-- info 地址 --> <property name="log.path" value="D:\log\" /> <property name="log.file" value="D:\log\logback.log" /> <!-- errr,错误路径 --> <property name="log.path.error" value="D:\log\error\" /> <property name="log.file.error" value="D:\log\error\logback-error.log" /> <!--警告路径 --> <property name="log.path.warn" value="D:\log\warn\" /> <property name="log.file.warn" value="D:\log\warn\logback-error.log" /> <include resource="org/springframework/boot/logging/logback/base.xml"/> <!-- 普通日志 --> <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.file}</file> <!-- 循环政策:基于时间创建日志文件 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志命名:单个文件大于128MB 按照时间+自增i 生成log文件 --> <fileNamePattern>${log.path}zlyPay-log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>512MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- 最大保存时间:30天--> <maxHistory>30</maxHistory> </rollingPolicy> <append>true</append> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> <charset>utf-8</charset> </encoder> <!-- 显示所有的日志记录 <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>info</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> --> </appender> <!-- 错误日志 --> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.file.error}</file> <!-- 循环政策:基于时间创建日志文件 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志命名:单个文件大于2MB 按照时间+自增i 生成log文件 --> <fileNamePattern>${log.path.error}zlyPay-log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>128MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- 最大保存时间:180天--> <maxHistory>180</maxHistory> </rollingPolicy> <append>true</append> <!-- 日志格式 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> <charset>utf-8</charset> </encoder> <!-- 日志级别过滤器 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤的级别 --> <level>ERROR</level> <!-- 匹配时的操作:接收(记录) --> <onMatch>ACCEPT</onMatch> <!-- 不匹配时的操作:拒绝(不记录) --> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 控制台 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- 日志格式 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> <charset>utf-8</charset> </encoder> <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <!-- 只有这个日志权限才能看,sql语句 --> <level>DEBUG</level> </filter> </appender> <!-- 警告信息 --> <appender name="STDOUT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 循环政策:基于时间创建日志文件 --> <file>${log.file.warn}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志命名:单个文件大于2MB 按照时间+自增i 生成log文件 --> <fileNamePattern>${log.path.warn}zlyPay-log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>128MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- 最大保存时间:180天--> <maxHistory>180</maxHistory> </rollingPolicy> <append>true</append> <!-- 日志格式 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--<Pattern>${ENCODER_PATTERN}</Pattern>--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %X{QTRACER} %X{MDCContextId}-%X{MDCSupplyService}-%X{MDCSupplyRetryTime} %-5level %logger{0} - %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>warn</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 输出sql日志 --> <logger name="org.jcut.dao" level="DEBUG"></logger> <!-- additivity 避免执行2次--> <logger name="com.cltx" level="INFO" additivity="false"> <appender-ref ref="STDOUT"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="ERROR_FILE"/> </logger> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="INFO_FILE" /> <appender-ref ref="ERROR_FILE" /> </root> </configuration>
3.application.properties文件中加入日志文件路径
#加载指定的日志配置文件 logging.config=classpath:logback-spring.xml #调试环境用DEBUG,生产环境用info logging.level.root=DEBUG