springboot自动清理日志

1. logback-spring.xml

在xml中rollingPolicy标签内加入以下:

<maxHistory>2maxHistory>
<cleanHistoryOnStart>truecleanHistoryOnStart>

完整的文件:


<configuration>

    
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    
    <springProperty scope="context" name="LOG_PATH" source="log.path" defaultValue="app-log"/>

    
    
    
    <springProfile name="local">
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${CONSOLE_LOG_PATTERN}pattern>
                <charset>utf-8charset>
            encoder>
        appender>

        
        <root level="info">
            <appender-ref ref="STDOUT"/>
        root>

        
        <logger name="cn.stylefeng" level="debug" additivity="false">
            <appender-ref ref="STDOUT"/>
        logger>
    springProfile>

    
    
    
    <springProfile name="!local">

        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${CONSOLE_LOG_PATTERN}pattern>
                <charset>utf-8charset>
            encoder>
        appender>

        
        <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">

            
            <file>${LOG_PATH}/log_error.logfile>

            
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                
                <fileNamePattern>${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>

                
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MBmaxFileSize>

                timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>2maxHistory>
                <cleanHistoryOnStart>truecleanHistoryOnStart>
            rollingPolicy>

            
            <append>trueappend>

            
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${FILE_LOG_PATTERN}pattern>
                <charset>utf-8charset>
            encoder>

            
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>errorlevel>
                <onMatch>ACCEPTonMatch>
                <onMismatch>DENYonMismatch>
            filter>
        appender>

        
        <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">

            
            <file>${LOG_PATH}/log_total.logfile>

            
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                
                <fileNamePattern>${LOG_PATH}/total/log-total-%d{yyyy-MM-dd}.%i.logfileNamePattern>

                
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MBmaxFileSize>

                timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>2maxHistory>
                <cleanHistoryOnStart>truecleanHistoryOnStart>
            rollingPolicy>

            
            <append>trueappend>

            
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${FILE_LOG_PATTERN}pattern>
                <charset>utf-8charset>
            encoder>
        appender>

        
        <root level="info">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="FILE_ERROR"/>
            <appender-ref ref="FILE_ALL"/>
        root>

    springProfile>
configuration>



2. logback.xml

我项目里的logback.xml也更改掉:


<configuration  scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logbackcontextName>
<springProperty scope="context" name="logLevel" source="logging.levels"/>
<springProperty scope="context" name="logPath" source="logging.path"/>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">

<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>


<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logPath}logback.%d{yyyy-MM-dd}.logfileNamePattern>
    <maxHistory>2maxHistory>
    <cleanHistoryOnStart>truecleanHistoryOnStart>
rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%npattern>
<charset>UTF-8charset>
encoder>
appender>


<root level="info">
<appender-ref ref="console" />
<appender-ref ref="file" />
root>
configuration>

你可能感兴趣的:(spring,boot,java,spring)