<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <!--
        Logback Configuration for Honey Backend
        This file supports runtime log level changes (scan="true" scanPeriod="30 seconds")
        For external file on VPS, use: -Dlogging.config=/path/to/logback-spring.xml
    -->

    <!-- Property: Log directory (configurable via LOG_DIR or defaults to ./logs) -->
    <property name="LOG_DIR" value="${LOG_DIR:-./logs}"/>
    
    <!-- Property: Application name -->
    <property name="APP_NAME" value="honey-be"/>

    <!-- Console Appender (for development/local) -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- File Appender with Rolling Policy -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/${APP_NAME}.log</file>
        
        <!-- Rolling Policy: Size and Time Based -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- File naming pattern: logs/app-2024-01-15.0.log -->
            <fileNamePattern>${LOG_DIR}/${APP_NAME}-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            
            <!-- Max file size: 50MB -->
            <maxFileSize>50MB</maxFileSize>
            
            <!-- Total history: 14 days -->
            <maxHistory>14</maxHistory>
            <timeUnit>days</timeUnit>
            
            <!-- Total size cap: 10GB -->
            <totalSizeCap>10GB</totalSizeCap>
            
            <!-- Clean history on startup -->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Async Appender (wraps FILE appender for non-blocking I/O) -->
    <appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Discard threshold: drop logs if queue is 80% full to prevent blocking -->
        <discardingThreshold>0</discardingThreshold>
        
        <!-- Queue size: 256 (good for high concurrency) -->
        <queueSize>256</queueSize>
        
        <!-- Never block: if queue is full, drop lower level logs (DEBUG/INFO) but keep WARN/ERROR -->
        <neverBlock>false</neverBlock>
        
        <!-- Include caller data: false for better performance -->
        <includeCallerData>false</includeCallerData>
        
        <!-- Reference to FILE appender -->
        <appender-ref ref="FILE"/>
    </appender>

    <!-- Logger Levels -->
    
    <!-- Root level: INFO (default for production) -->
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ASYNC_FILE"/>
    </root>

    <!-- Application package: INFO (can be changed to DEBUG at runtime) -->
    <logger name="com.honey" level="INFO"/>

    <!-- Noise Reduction: Infrastructure packages at WARN -->
    <logger name="org.springframework" level="WARN"/>
    <logger name="org.springframework.web" level="WARN"/>
    <logger name="org.springframework.web.socket" level="WARN"/>
    <logger name="org.springframework.messaging" level="WARN"/>
    <logger name="org.hibernate" level="WARN"/>
    <logger name="org.hibernate.SQL" level="WARN"/>
    <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="WARN"/>
    <logger name="com.zaxxer.hikari" level="WARN"/>
    <logger name="org.apache.tomcat" level="WARN"/>
    <logger name="org.apache.catalina" level="WARN"/>
    <logger name="io.netty" level="WARN"/>
    <logger name="org.springframework.boot.autoconfigure" level="WARN"/>

    <!-- Keep Spring Boot config/env at INFO for startup debugging -->
    <logger name="org.springframework.boot.context.config" level="INFO"/>
    <logger name="org.springframework.core.env" level="INFO"/>

</configuration>


