<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <springProperty scope="context" name="LOG_LEVEL" source="logging.level.root" defaultValue="info"/>
    <springProperty scope="context" name="MAX_FILE_SIZE" source="logging.max_file_size" defaultValue="128MB"/>
    <springProperty scope="context" name="MAX_HISTORY" source="log.max_history" defaultValue="7"/>
    <springProperty scope="context" name="PATTERN" source="log.pattern"
                    defaultValue="-|%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%X{tid}|%thread|%logger{36}.%M:%L-%msg%n"/>

    <property name="LOG_NAME" value="equip"/>
    <property name="LOG_PATH" value="./logs"/>
    <property name="LOG_DIR" value="${LOG_PATH}/${LOG_NAME}/%d{yyyyMMdd}"/>
    <property name="CHARSET" value="UTF-8"/>

    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${PATTERN}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>  <!-- 只记录error级别的日志 -->
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <!-- 定义文件的名称 -->
        <file>${LOG_PATH}/${LOG_NAME}/error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_DIR}/err_${LOG_NAME}%i.log</FileNamePattern>
            <MaxHistory>${MAX_HISTORY}</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>${PATTERN}</Pattern>
        </layout>
    </appender>

    <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 定义文件的名称 -->
        <file>${LOG_PATH}/${LOG_NAME}/total.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_DIR}/all_${LOG_NAME}%i.log</FileNamePattern>
            <MaxHistory>${MAX_HISTORY}</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${PATTERN}</pattern>
        </layout>
    </appender>

    <appender name="ASYNC_STDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT"/>
        <queueSize>256</queueSize>
        <neverBlock>true</neverBlock>
        <includeCallerData>true</includeCallerData>
    </appender>

    <appender name="ASYNC_FILE_ALL" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="FILE_ALL"/>
        <queueSize>1024</queueSize>
        <neverBlock>true</neverBlock>
        <includeCallerData>true</includeCallerData>
    </appender>

    <appender name="ASYNC_ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="ERROR"/>
        <queueSize>256</queueSize>
        <neverBlock>true</neverBlock>
        <includeCallerData>true</includeCallerData>
    </appender>

    <!--统一日志输出级别，其他appender中如果有高于此处等级设置的也会被输出 -->
    <root level="${LOG_LEVEL}">
        <springProfile name="test,dev">
            <appender-ref ref="ASYNC_STDOUT"/>
        </springProfile>
        <appender-ref ref="ASYNC_FILE_ALL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </root>
</configuration>