<?xml version="1.0" encoding="UTF-8"?>

<!--please pay attention that: file name should not be logback.xml，name it logback-spring.xml to use it in springboot framework-->
<configuration>

    <springProperty scope="context" name="logging.path" source="logging.path"/>
    <springProperty scope="context" name="logging.level" source="logging.level.com.alipay.websofademo"/>


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{80} - %msg%n</Pattern>
        </encoder>
    </appender>

    <!-- to generate logfile daily -->
    <appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <!-- a filter that show green light for object that has a error log level-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>error</level>
        </filter>
        <!-- log name -->
        <file>${logging.path}/websofademo/common-error.log</file>
        <!-- to generate a log file everyday with a longest lasting of 30 days -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- logfile name with daily rolling-->
            <FileNamePattern>${logging.path}/websofademo/common-error.log.%d{yyyy-MM-dd}</FileNamePattern>
            <!-- log perserve days-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--output format：%d is for date，%thread is for thread name，%-5level：loglevel with 5 character  %msg：log message，%n line breaker-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <!-- encoding -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="ROOT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${logging.level}</level>
        </filter>
        <file>${logging.path}/websofademo/common-default.log</file>
        <!-- to generate a log file everyday with a longest lasting of 30 days -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- logfile name with daily rolling-->
            <FileNamePattern>${logging.path}/websofademo/common-default.log.%d{yyyy-MM-dd}
            </FileNamePattern>
            <!-- log perserve days-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--output format：%d is for date，%thread is for thread name，%-5level：loglevel with 5 character  %msg：log message，%n line breaker-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <!-- encoding -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- tracer mdc example, you can delete it -->
    <appender name="MDC-EXAMPLE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${logging.level}</level>
        </filter>
        <file>${logging.path}/websofademo/mdc-example.log</file>
        <!-- to generate a log file everyday with a longest lasting of 30 days -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- logfile name with daily rolling-->
            <FileNamePattern>${logging.path}/websofademo/mdc-example.log.%d{yyyy-MM-dd}
            </FileNamePattern>
            <!-- log perserve days-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--output format：%d is for date，%thread is for thread name，%-5level：loglevel with 5 character  %msg：log message，%n line breaker-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{SOFA-TraceId},%X{SOFA-SpanId}]  %logger{50} - %msg%n</pattern>
            <!-- encoding -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <springProfile name="dev">
        <!-- spring.profiles.active contains dev profile -->
        <appender name="DEV-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <append>true</append>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>${logging.level}</level>
            </filter>
            <file>${logging.path}/websofademo/websofademo-default-dev.log</file>
            <!-- to generate a log file everyday with a longest lasting of 30 days -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- logfile name with daily rolling-->
                <FileNamePattern>
                    ${logging.path}/websofademo/websofademo-default-dev.log.%d{yyyy-MM-dd}
                </FileNamePattern>
                <!-- log perserve days-->
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--output format：%d is for date，%thread is for thread name，%-5level：loglevel with 5 character  %msg：log message，%n line breaker-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
                <!-- encoding -->
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    </springProfile>

    <springProfile name="test">
        <!-- spring.profiles.active contains test profile -->
        <appender name="TEST-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <append>true</append>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>${logging.level}</level>
            </filter>
            <file>${logging.path}/websofademo/websofademo-default-test.log</file>
            <!-- to generate a log file everyday with a longest lasting of 30 days -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- logfile name with daily rolling-->
                <FileNamePattern>
                    ${logging.path}/websofademo/websofademo-default-test.log.%d{yyyy-MM-dd}
                </FileNamePattern>
                <!-- log perserve days-->
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--output format：%d is for date，%thread is for thread name，%-5level：loglevel with 5 character  %msg：log message，%n line breaker-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
                <!-- encoding -->
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    </springProfile>

    <logger name="com.alipay.websofademo" level="${logging.level}" additivity="false">
        <appender-ref ref="ROOT-APPENDER"/>
        <appender-ref ref="ERROR-APPENDER"/>
    </logger>

    <logger name="MDC-EXAMPLE" level="${logging.level}" additivity="false">
        <appender-ref ref="MDC-EXAMPLE-APPENDER"/>
    </logger>

    <root level="${logging.level}">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="ROOT-APPENDER"/>
        <appender-ref ref="ERROR-APPENDER"/>
    </root>

</configuration>