SpringBoot日志文件分隔(根据日期和文件大小)

1.首先resource目录下新建文件 logback-spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://logback.qos.ch/manual/appenders.html 官网 -->
<configuration scan="true">
    <contextName>test</contextName>
    <property name="LOGPATH" value="${LOG_FILE}" />
    <!-- 输出到控制台 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                [%date{yyyy-MM-dd HH:mm:ss.SSS}] %X{logthreadId} %-5level %logger{80} %method %line - %msg%n
            </pattern>
        </layout>
    </appender>

    <!-- 输出到文件 -->
    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGPATH}${file.separator}current.log</file>
        <append>true</append>
        <encoder>
            <pattern>
                [%date{yyyy-MM-dd HH:mm:ss.SSS}] %X{logthreadId} %-5level %logger{80} %method  %line - %msg%n
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOGPATH}${file.separator}%d{yyyy-MM-dd}${file.separator}%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--保留天数-->
            <maxHistory>15</maxHistory>
            <!--分隔的每个文件的大小-->
            <maxFileSize>500KB</maxFileSize>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="fileLog" />
        <appender-ref ref="stdout"  />
    </root>
</configuration>

2.在yml中做配置

logging:
  file:
    name: ${logging.file.path}
    path: "C:/te/pocLog/"
  level:
    com.baomidou.mybatisplus: DEBUG
    com.zuiwen.boot.mapper: DEBUG

logging.file.path为输出路径

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