<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--定义日志文件的存储路径-->
    <!--<property name="LOG_HOME" value="/var/tmp/website-blog-web"/>-->
    <springProperty scope="context" name="logdir" source="logging.path"/>

    <!-- 控制台 appender -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] %-5level - %msg%n</pattern>
        </encoder>
    </appender>

    <!--按天生成日志-->
    <!-- 出错日志 appender -->
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logdir}/error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${logdir}/error-%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <!-- 日志最大的历史 30天 -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] %-5level - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!-- info日志 appender -->
    <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logdir}/info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${logdir}/info-%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <!-- 日志最大的历史 30天 -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] %-5level - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <logger name="org.springframework.core.env" level="ERROR"/>

    <!-- 测试环境+开发环境，日志级别为INFO且不写日志文件 -->
    <springProfile name="test,dev">
        <logger name="com.zyd.blog" additivity="false">
            <appender-ref ref="STDOUT"/>
        </logger>
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        </root>
    </springProfile>

    <!-- 生产环境. 日志级别为WARN且写日志文件-->
    <springProfile name="prod">
        <logger name="com.zyd.blog" additivity="false">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="FILE_ERROR" />
            <appender-ref ref="FILE_INFO" />
        </logger>

        <root level="WARN">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="FILE_ERROR" />
            <appender-ref ref="FILE_INFO" />
        </root>
    </springProfile>

</configuration>  