<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_HOME" value="./logs" />
    <property name="APP_NAME" value="gdt-auto" />

    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <springProperty scope="context" name="plumelog.env" source="plumelog.env"/>
    <springProperty scope="context" name="plumelog.redis.host" source="plumelog.redis.host"/>
    <springProperty scope="context" name="plumelog.redis.port" source="plumelog.redis.port"/>
    <springProperty scope="context" name="plumelog.redis.auth" source="plumelog.redis.auth"/>


    <!-- 控制台输出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 按照每天生成日志文件 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${APP_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 错误日志文件 -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${APP_NAME}-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.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}-error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- Plumelog Appender -->
   <appender name="plumelog" class="com.plumelog.logback.appender.RedisAppender">
        <appName>${springAppName}</appName>
        <redisHost>${plumelog.redis.host}</redisHost>
        <redisPort>${plumelog.redis.port}</redisPort>
        <redisAuth>${plumelog.redis.auth}</redisAuth>
        <redisDb>0</redisDb>
        <env>${plumelog.env}</env>
    </appender>
    
    <!-- SQL日志 -->
    <logger name="com.moka.gdtauto.mapper" level="DEBUG"/>
    <logger name="p6spy" level="INFO"/>
    <!-- MyBatis日志 -->
    <logger name="org.apache.ibatis" level="INFO"/>
    <logger name="com.baomidou.mybatisplus" level="INFO"/>
    <!-- 数据库连接池日志 -->
    <logger name="com.zaxxer.hikari" level="INFO"/>

    <!-- 本地开发环境 -->
    <springProfile name="local">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>

    <!-- 开发环境 -->
    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="FILE"/>
            <appender-ref ref="ERROR_FILE"/>
        </root>
    </springProfile>

    <!-- 生产环境 -->
    <springProfile name="pro">
        <root level="INFO">
            <appender-ref ref="plumelog"/>
        </root>
    </springProfile>

    <springProfile name="mcp">
        <root level="INFO">
            <appender-ref ref="plumelog"/>
        </root>
    </springProfile>

    <!-- 默认配置 -->
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>
