<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
    <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>

    <property name="PROJECT_NAME"    value="tsi-comm-server"/>
    <property name="ROOT_LOG_LEVEL"  value="DEBUG"/>
    <property name="LOG_CHARSET"     value="UTF-8" />

    <springProperty scope="context" name="LOGGING_PATH" source="logging.file.path"/>
    <property name="LOG_PATH" value="${LOGGING_PATH:-${user.dir}/logs/${PROJECT_NAME}}/"/>
    <property name="LOG_BACKUP_PATH" value="${LOG_PATH}/backup/"/>

    <property name="LOG_FILE_NAME"             value="${PROJECT_NAME}.log"/>
    <property name="LOG_FILE_NAME_ERROR"       value="${PROJECT_NAME}.err.log"/>
    <property name="LOG_FILE_NAME_PATTERN"     value="%d{yyyyMMdd}_%i.log.gz"/>

    <property name="LOG_FILE_NAME_PROCESS"     value="process"/>
    <property name="LOG_FILE_NAME_RAW"         value="raw"/>

    <property name="LOG_FILE_NAME_SESSION"     value="session.log"/>
    <property name="LOG_FILE_NAME_SCHEDULE"    value="schedule.log"/>
    <property name="LOG_FILE_NAME_REPORT"      value="report.log"/>
    <property name="LOG_FILE_NAME_CONFIG"      value="config.log"/>
    <property name="LOG_FILE_NAME_DBMS"        value="dbms.log"/>
    <property name="LOG_FILE_NAME_KAFKA"       value="kafka.log"/>

    <property name="MAX_FILESIZE" value="20MB"/>
    <property name="MAX_HISTORY"  value="15"/>
    <property name="MAX_PROCESS_FILESIZE" value="20MB"/>

    <property name="LOG_PATTERN_FILE"        value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %msg%n"/>
    <property name="LOG_PATTERN_ERROR"       value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] %msg%n"/>
    <property name="LOG_PATTERN_PROCESS"     value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %msg%n"/>
    <property name="LOG_PATTERN_RAW"         value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %msg%n"/>
    <property name="LOG_PATTERN_CONSOLE"     value="[%d{HH:mm:ss.SSS}] %highlight([%5level]) %highlight(${PID:-}): %cyan(%msg) %n"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_CONSOLE}</pattern>
        </encoder>
    </appender>

    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${LOG_PATH}${LOG_FILE_NAME_ERROR}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_ERROR}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_ERROR}.%d{yyyy-MM-dd}_%i.log</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <appender name="FILE_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <appender name="FILE_PROCESS" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>id</key>
            <defaultValue>${LOG_FILE_NAME_PROCESS}</defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE-${id}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_PATH}process/${id}.log</file>
                <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                    <charset>${LOG_CHARSET}</charset>
                    <Pattern>${LOG_PATTERN_PROCESS}</Pattern>
                </encoder>

                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <FileNamePattern>${LOG_BACKUP_PATH}process/${id}.${LOG_FILE_NAME_PATTERN}</FileNamePattern>
                    <maxFileSize>${MAX_PROCESS_FILESIZE}</maxFileSize>
                    <maxHistory>${MAX_HISTORY}</maxHistory>
                </rollingPolicy>
            </appender>
        </sift>
    </appender>

    <logger name="com.tsi.comm.server.protocol" level="INFO" additivity="false">
        <appender-ref ref="FILE_PROCESS"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>
    <logger name="com.tsi.comm.server.process.packet" level="INFO" additivity="false">
        <appender-ref ref="FILE_PROCESS"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>
    <logger name="com.tsi.comm.server.process.logging" level="INFO" additivity="false">
        <appender-ref ref="FILE_PROCESS"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>
    <logger name="com.tsi.comm.server.tcp.handler.MdcLoggingHandler" level="INFO" additivity="false">
        <appender-ref ref="FILE_PROCESS"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <root level="INFO">
<!--        <appender-ref ref="CONSOLE"/>-->
        <appender-ref ref="FILE_LOG"/>
        <appender-ref ref="FILE_ERROR"/>
    </root>

    <appender name="FILE_CONFIG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_CONFIG}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_CONFIG}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <logger name="com.tsi.comm.server.config" level="INFO" additivity="true">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_CONFIG"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>
    <logger name="com.tsi.comm.server.TsiCommServerApplication" level="INFO" additivity="true">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_CONFIG"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <appender name="FILE_DBMS" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_DBMS}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_DBMS}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>
    <logger name="com.tsi.comm.server.process.dbms" level="INFO" additivity="false">
        <appender-ref ref="FILE_DBMS"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <appender name="FILE_KAFKA" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_KAFKA}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_KAFKA}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>
    <logger name="com.tsi.comm.server.kafka" level="INFO" additivity="false">
        <appender-ref ref="FILE_KAFKA"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <appender name="FILE_SESSION" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_SESSION}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_SESSION}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <logger name="com.tsi.comm.server.tcp" level="INFO" additivity="false">
        <appender-ref ref="FILE_SESSION"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <appender name="FILE_REPORT" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>filename</key>
            <defaultValue>${LOG_FILE_NAME_REPORT}</defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE-${filename}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_PATH}/${filename}.log</file>
                <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                    <charset>${LOG_CHARSET}</charset>
                    <Pattern>${LOG_PATTERN_PROCESS}</Pattern>
                </encoder>

                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <FileNamePattern>${LOG_BACKUP_PATH}/${filename}.${LOG_FILE_NAME_PATTERN}</FileNamePattern>
                    <maxFileSize>${MAX_FILESIZE}</maxFileSize>
                    <maxHistory>${MAX_HISTORY}</maxHistory>
                </rollingPolicy>
            </appender>
        </sift>
    </appender>

    <logger name="com.tsi.comm.server.repository" level="INFO" additivity="false">
        <appender-ref ref="FILE_REPORT"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

    <appender name="FILE_SCHEDULE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_SCHEDULE}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_SCHEDULE}.${LOG_FILE_NAME_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <logger name="com.tsi.comm.server.scheduler" level="INFO" additivity="false">
        <appender-ref ref="FILE_SCHEDULE"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

</configuration>
