<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

  	<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>

	<!-- ===================================================== -->
	<!-- Service Config -->
	<!-- ===================================================== -->
  	<property name="SERVICE_LOG_PATTERN"
              value="%-16X{traceId} %-12X{clientId:--} %-16X{method} %-25logger{0} %msg"/>

    <!-- 彩色日志 -->
    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!-- 开发测试环境 -->
    <springProfile name="test,dev,local,sit,uat">
        <!-- 每天产生一个文件 -->
        <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.service.output:-service.log}</file>
            <!--日志文件输出格式-->
            <encoder>
                <pattern>%date %.-3level ${SERVICE_LOG_PATTERN}%n</pattern>
                <charset>UTF-8</charset> <!-- 此处设置字符集 -->
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.service.output:-service.log}-%d{yyyy-MM-dd}.%i</fileNamePattern>

                <!-- 启动服务时，是否清理历史日志，一般不建议清理 -->
                <cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-true}</cleanHistoryOnStart>

                <!-- 文件大小 -->
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
			 		<maxFileSize>1MB</maxFileSize>
				</timeBasedFileNamingAndTriggeringPolicy>
                <!-- 文件最大保存历史数量 -->
                <MaxHistory>1</MaxHistory>
            </rollingPolicy>

            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%date %.-3level ${SERVICE_LOG_PATTERN}%n</pattern>
            </layout>
        </appender>

        <!--输出到控制台-->
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
                <!-- 设置字符集 -->
                <charset>UTF-8</charset>
            </encoder>
        </appender>

        <root level="INFO">
            <appender-ref ref="TEST-FILE" />
            <appender-ref ref="CONSOLE" />
        </root>
    </springProfile>

    <!-- 生产环境. -->
    <springProfile name="pro">
        <!-- 每天产生一个文件 -->
        <appender name="PRO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.service.output:-service.log}</file>
            <!--日志文件输出格式-->
            <encoder>
                <pattern>%date %.-3level ${SERVICE_LOG_PATTERN}%n</pattern>
                <charset>UTF-8</charset> <!-- 此处设置字符集 -->
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.service.output:-service.log}-%d{yyyy-MM-dd}.%i</fileNamePattern>
                <!-- 文件大小 -->
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
			 		<maxFileSize>50MB</maxFileSize>
				</timeBasedFileNamingAndTriggeringPolicy>
                <!-- 文件最大保存历史数量 -->
                <MaxHistory>10</MaxHistory>
            </rollingPolicy>

            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%date %.-3level ${SERVICE_LOG_PATTERN}%n</pattern>
            </layout>
        </appender>

        <root level="INFO">
            <appender-ref ref="PRO-FILE" />
        </root>
    </springProfile>
</configuration>
