<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <springProperty scope="context" name="SPRING_APP_NAME" source="spring.application.name" />
    <springProperty scope="context" name="LOGGER_ROOT_LEVEL" source="logger.root.level" />
    <springProperty scope="context" name="LOGGER_ROOT_PATH" source="logger.root.path" />
    <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
    <!-- appender是configuration的子节点，是负责写日志的组件。 -->
    <!-- ConsoleAppender：把日志输出到控制台 -->
    <logger name="org.springframework" level="INFO" />
    <logger name="com.alibaba" level="INFO" />
    <logger name="org.apache" level="INFO" />
    <logger name="org.hibernate" level="INFO" />
    <logger name="springfox" level="INFO" />
    <logger name="com.baomidou" level="INFO" />
    <logger name="org.flywaydb" level="INFO" />
    <logger name="io.lettuce" level="INFO" />
    <logger name="com.zaxxer" level="INFO" />
    <logger name="org.ehcache" level="INFO" />

    <property name="LOG_PATH" value="${LOGGER_ROOT_PATH}/${SPRING_APP_NAME}" />
    <property name="MAX_HISTORY" value="10" />
    <property name="MAX_FILE_SIZE" value="200MB" />
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} -%msg%n" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d %p (%file:%line\)- %m%n</pattern>
            <!-- 控制台也要使用UTF-8，不要使用GBK，否则会中文乱码 -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="FILE_ALL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/logger.log</File>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_PATH}/logger-%d{yyyyMMddHH}.log.%i</FileNamePattern>
            <!--日志文件保留天数 -->
            <MaxHistory>${MAX_HISTORY}</MaxHistory>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
    </appender>

    <!--日志文件配置error -->
    <appender name="FILE_ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/error.log</File>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_PATH}/error-%d{yyyyMMddHH}.log.%i</FileNamePattern>
            <!--日志文件保留天数 -->
            <MaxHistory>${MAX_HISTORY}</MaxHistory>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
    </appender>

    <!-- 为logstash输出的JSON格式的Appender -->
    <!--    <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">-->
    <!--        <destination>${elkLoggerUrl}</destination>-->
    <!--        &lt;!&ndash; 日志输出编码 &ndash;&gt;-->
    <!--        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">-->
    <!--            <providers>-->
    <!--                <mdc/>-->
    <!--                <timestamp>-->
    <!--                    <timeZone>UTC</timeZone>-->
    <!--                </timestamp>-->
    <!--                <pattern>-->
    <!--                    <pattern>-->
    <!--                        {-->
    <!--                        "severity": "%level",-->
    <!--                        "applicationName": "${SPRING_APP_NAME:-}",-->
    <!--                        "requestId": "%X{requestId:-}",-->
    <!--                        "pid": "${PID:-}",-->
    <!--                        "thread": "%thread",-->
    <!--                        "class": "%logger{40}",-->
    <!--                        "message": "%message"-->
    <!--                        }-->
    <!--                    </pattern>-->
    <!--                </pattern>-->
    <!--                <stackTrace>-->
    <!--                    <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">-->
    <!--                        <maxDepthPerThrowable>50</maxDepthPerThrowable>-->
    <!--                        <maxLength>4048</maxLength>-->
    <!--                        <shortenedClassNameLength>50</shortenedClassNameLength>-->
    <!--                        <exclude>^sun\.reflect\..*\.invoke</exclude>-->
    <!--                        <exclude>^net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>-->
    <!--                        <rootCauseFirst>true</rootCauseFirst>-->
    <!--                    </throwableConverter>-->
    <!--                </stackTrace>-->
    <!--            </providers>-->
    <!--        </encoder>-->
    <!--    </appender>-->
    <!-- 控制台输出日志级别 -->
    <!--    <springProfile name="dev">-->
    <!--        <property name="LOG_ROOT_LEVEL" value="DEBUG"/>-->
    <!--    </springProfile>-->
    <!--    <springProfile name="prod">-->
    <!--        <property name="LOG_ROOT_LEVEL" value="INFO"/>-->
    <!--    </springProfile>-->

    <root level="${LOGGER_ROOT_LEVEL}">
        <appender-ref ref="FILE_ALL_LOG" />
        <appender-ref ref="FILE_ERROR_LOG" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>