<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <springProperty scope="context" name="LOG_FILE_NAME" source="spring.application.name"/>

    <!-- 日志存储目录 -->
    <property name="LOG_FILE_PATH" value="./logs/" />
    <!-- 日志文件大小 -->
    <property name="MAX_FILE_SIZE" value="100MB" />
    <!-- 日志最大的历史（单位：天） -->
    <property name="MAX_HISTORY" value="90" />

    <!-- 格式化输出：%d表示日期，%thread表示线程名，%-5level：级别从左显示5个字符宽度，%msg：日志消息，%n：换行符-->
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread-%X{traceId}] %-5level %logger{50}.%M[%line]：%msg%n" />

    <!-- 控制台输出 -->
    <appender name="CONSOLE_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <Pattern>${LOG_PATTERN}</Pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 滚动记录文件，先将日志记录到指定文件，当符合某个条件时，将日志记录到其他文件 -->
    <appender name="FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_FILE_PATH}${LOG_FILE_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE_PATH}${LOG_FILE_NAME}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- mybatis sql日志 日志的级别需要是DEBUG -->
    <!-- 日志打印的包的范围，及分类日志文件存储 -->
    <logger name="com.esv" level="DEBUG" additivity="false">
        <appender-ref ref="CONSOLE_APPENDER" />
        <appender-ref ref="FILE_APPENDER" />
    </logger>

    <!--控制台和日志文件输出级别-->
    <root level="INFO" additivity="false">
        <appender-ref ref="CONSOLE_APPENDER" />
        <appender-ref ref="FILE_APPENDER" />
    </root>

</configuration>