Springboot配置 logback日志框架,且自动清理生效

依赖版本

<dependency>
   <groupId>ch.qos.logbackgroupId>
   <artifactId>logback-classicartifactId>
   <version>1.2.3version>
dependency>
<dependency>
   <groupId>ch.qos.logbackgroupId>
   <artifactId>logback-coreartifactId>
   <version>1.2.3version>
dependency>

logback-spring.xml




<configuration>
    
    <springProperty scope="context" name="logging.profile" source="spring.profiles.active"/>
    <springProperty scope="context" name="logging.path" source="logging.path"/>
    <springProperty scope="context" name="logging.level" source="logging.level"/>
    <springProperty scope="context" name="logging.name" source="spring.application.name"/>
    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{80} - %msg%nPattern>
        encoder>
    appender>
    
    <appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>errorlevel>
        filter>
        <file>${logging.path}/${logging.name}-error-${logging.profile}.log}file>
        
        
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <FileNamePattern>
                ${logging.path}/${logging.name}-error.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            
            <maxFileSize>10MBmaxFileSize>
            <maxHistory>7maxHistory>
            
            <totalSizeCap>500MBtotalSizeCap>
            
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            
            <charset>UTF-8charset>
        encoder>
    appender>
    
    <appender name="HTML-APPENDER" class="ch.qos.logback.core.FileAppender">
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>errorlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%p%d%msg%M%F{32}%Lpattern>
            layout>
            
            <charset>UTF-8charset>
        encoder>
        <file>${logging.path}/${logging.name}-error-log.htmlfile>
    appender>
    <appender name="COMMON-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${logging.level}level>
        filter>
        <file>${logging.path}/${logging.name}-${logging.profile}.log}file>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <FileNamePattern>
                ${logging.path}/${logging.name}-${logging.profile}.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            <maxFileSize>10MBmaxFileSize>
            <maxHistory>2maxHistory>
            <totalSizeCap>100MBtotalSizeCap>
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            
            <charset>UTF-8charset>
        encoder>
    appender>

    
    <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logging.path}/${logging.name}-all.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>
                ${logging.path}/${logging.name}-all.%d{yyyy-MM-dd}.%i.zip
            FileNamePattern>
            <maxFileSize>10MBmaxFileSize>
            <maxHistory>7maxHistory>
            <totalSizeCap>500MBtotalSizeCap>
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-40.40logger{39} : %m%npattern>
        encoder>
        <append>falseappend>
    appender>
    <springProfile name="dev">
        
        <logger name="com.apache.ibatis" level="DEBUG"/>
        <logger name="java.sql.Connection" level="DEBUG"/>
        <logger name="java.sql.Statement" level="DEBUG"/>
        <logger name="java.sql.PreparedStatement" level="DEBUG"/>
        <logger name="org.springframework" level="error"/>
		
        <logger name="com.gs.quzhou.daping" level="${logging.level}" additivity="false">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="ERROR-APPENDER"/>
        logger>
        <root level="${logging.level}">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="ERROR-APPENDER"/>
            <appender-ref ref="FILE_ALL"/>
        root>
    springProfile>
    <springProfile name="test">
        
        <logger name="com.apache.ibatis" level="INFO"/>
        <logger name="java.sql.Connection" level="INFO"/>
        <logger name="java.sql.Statement" level="INFO"/>
        <logger name="java.sql.PreparedStatement" level="DEBUG"/>
         
        <logger name="com.gs.quzhou.daping" level="${logging.level}" additivity="false">
            <appender-ref ref="COMMON-APPENDER"/>
            <appender-ref ref="ERROR-APPENDER"/>
            <appender-ref ref="FILE_ALL"/>
        logger>

        <root level="${logging.level}">
            <appender-ref ref="COMMON-APPENDER"/>
            <appender-ref ref="ERROR-APPENDER"/>
        root>
    springProfile>
    <springProfile name="prod">
        
        <logger name="com.apache.ibatis" level="WARN"/>
        <logger name="java.sql.Connection" level="WARN"/>
        <logger name="java.sql.Statement" level="WARN"/>
        <logger name="java.sql.PreparedStatement" level="WARN"/>
        
        <logger name="com.gs.quzhou.daping" level="${logging.level}" additivity="false">
            <appender-ref ref="COMMON-APPENDER"/>
            <appender-ref ref="ERROR-APPENDER"/>
        logger>
        <root level="${logging.level}">
            <appender-ref ref="COMMON-APPENDER"/>
            <appender-ref ref="HTML-APPENDER"/>
            <appender-ref ref="ERROR-APPENDER"/>
        root>
    springProfile>
configuration>

yml 配置

spring:
  application:
    name: xxxx
  profiles:
    active: dev
logging:
  # logback-spring.xml 配置
  config: classpath:logback/logback-spring.xml
  # 配置日志文件保存路径
  path: D:\\project\\logs
  pattern:
    file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
  level:
    root: debug

自动清理不生效参考博文一
自动清理不生效参考博文二

你可能感兴趣的:(笔记,log4j)