<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 日志目录，线上默认 /var/log/pharmacopoeia -->
    <property name="LOG_DIR" value="${LOG_DIR:-/var/log/pharmacopoeia}"/>
    <!-- 单个文件最大 100MB -->
    <property name="MAX_SIZE" value="100MB"/>
    <!-- 保留最近 30 天的日志 -->
    <property name="MAX_HISTORY" value="30"/>
    <!-- 总大小上限 10GB -->
    <property name="TOTAL_SIZE" value="10GB"/>

    <!-- 控制台输出（开发调试用） -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] [%X{ip}] %logger{20}.%M\(%line\) - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 全量日志 -->
    <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/backend.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_DIR}/backend.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>${MAX_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <totalSizeCap>${TOTAL_SIZE}</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{ip}] %logger{20}.%M\(%line\) - %msg%n</pattern>
            <charset>UTF-8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
    </appender>

    <!-- 错误日志 -->
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/backend-error.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_DIR}/backend-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>${MAX_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <totalSizeCap>512MB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{ip}] %logger{20}.%M\(%line\) - %msg%n</pattern>
            <charset>UTF-8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
    </appender>

    <!-- 业务日志：com.pharmacopoeia 包 INFO 级别 -->
    <logger name="com.pharmacopoeia" level="INFO" additivity="false">
        <appender-ref ref="FILE_ALL"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <!-- Spring/框架日志：WARN 级别，减少噪音 -->
    <logger name="org.springframework" level="WARN"/>
    <logger name="org.hibernate" level="WARN"/>
    <logger name="com.zaxxer.hikari" level="WARN"/>

    <!-- 根日志：只输出 Spring/框架/WARN 控台 -->
    <root level="WARN">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>
