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

    <!-- 测试环境下明确指定日志配置，避免IS_UNDEFINED问题 -->
    <property name="log.projectName" value="entrance"/>
    <property name="logHomeDir" value="D:\logs"/>
    <property name="logCommonLevel" value="info"/>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="ec"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />


    <!--日志输出格式-->
    <property name="log.pattern"
              value="|-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%level} ${log.projectName} -- %t %logger{100}.%M ==> %m%n${Log_EXCEPTION_CONVERSION_WORD:-%ec}}}"/>
    <property name="log.maxHistory" value="30"/>

    <!--客户端输出日志-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--系统中常规的debug日志-->
    <!-- 滚动记录文件，先将日志记录到指定文件，当符合某个条件时，将日志记录到其他文件 RollingFileAppender -->
    <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>
            ${logHomeDir}/${log.projectName}/debug/debug.log
        </file>
        <!-- 如果日志级别等于配置级别，过滤器会根据onMath 和 onMismatch接收或拒绝日志。 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 设置过滤级别 -->
            <level>DEBUG</level>
            <!-- 用于配置符合过滤条件的操作 -->
            <onMatch>ACCEPT</onMatch>
            <!-- 用于配置不符合过滤条件的操作 -->
            <onMismatch>DENY</onMismatch>
        </filter>
        <!-- 最常用的滚动策略，它根据时间来制定滚动策略.既负责滚动也负责触发滚动  SizeAndTimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志输出位置 可相对、和绝对路径 -->
            <fileNamePattern>
                ${logHomeDir}/${log.projectName}/debug/debug.log.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <!-- 可选节点，控制保留的归档文件的最大数量，超出数量就删除旧文件,假设设置每个月滚动，且<maxHistory>是6，
            则只保存最近6个月的文件，删除之前的旧文件。注意，删除旧文件是，那些为了归档而创建的目录也会被删除 -->
            <maxHistory>${log.maxHistory:-30}</maxHistory>
            <!--重启清理日志文件-->
            <!--            <cleanHistoryOnStart>true</cleanHistoryOnStart>-->
            <!--每个文件最多100MB，保留N天的历史记录，但最多20GB-->
            <!--<totalSizeCap>20GB</totalSizeCap>-->
            <!--日志文件最大的大小-->
            <!--<MaxFileSize>${log.maxSize}</MaxFileSize>-->
        </rollingPolicy>
        <encoder>
            <pattern>
                ${log.pattern}
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--系统中常规的info日志-->
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>
            ${logHomeDir}/${log.projectName}/info/info.log
        </file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>
                ${logHomeDir}/${log.projectName}/info/info.log.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>${log.maxHistory:-30}</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                ${log.pattern}
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>


    <!--系统中常规的error日志-->
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>
            ${logHomeDir}/${log.projectName}/error/error.log
        </file>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>
                ${logHomeDir}/${log.projectName}/error/error.log.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>${log.maxHistory:-30}</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                ${log.pattern}
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <logger name="org.apache.catalina.startup.DigesterFactory"  level="ERROR"/>
    <logger name="org.apache.catalina.util.LifecycleBase"  level="ERROR"/>
    <logger name="org.apache.coyote.http11.Http11NioProtocol"  level="WARN"/>



    <logger name="com.njcn" level="INFO" additivity="false">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="DEBUG"/>
        <appender-ref ref="INFO"/>
        <appender-ref ref="ERROR"/>
    </logger>

    <root level="${logCommonLevel}">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="DEBUG"/>
        <appender-ref ref="INFO"/>
        <appender-ref ref="ERROR"/>
    </root>
</configuration>