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

    <property name="APP_CLASS_PATH"  value="com.utic.center.utic.ptis.server"/>
    <property name="PROJECT_PREFIX"  value="utic-ptis"/>
    <property name="PROJECT_NAME"    value="${PROJECT_PREFIX}-server"/>
    <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_HEALTH" value="${PROJECT_PREFIX}-health.log"/>
    <property name="LOG_FILE_NAME_BACKUP" value="%d{yyyyMMdd}_%i.log.gz"/>

    <property name="MAX_FILESIZE" value="40MB"/>
    <property name="MAX_HISTORY"  value="10"/>
    <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] %msg%n"/>
    <property name="LOG_PATTERN_HEALTH"      value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %msg%n"/>
    <property name="LOG_PATTERN_CONSOLE"     value="[%d{HH:mm:ss.SSS}] [%5level] %msg %n"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_CONSOLE}</pattern>
        </encoder>
    </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_BACKUP}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </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}.${LOG_FILE_NAME_BACKUP}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <appender name="FILE_HEALTH" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}${LOG_FILE_NAME_HEALTH}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <charset>${FILE_LOG_CHARSET}</charset>
            <pattern>${LOG_PATTERN_HEALTH}</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_BACKUP_PATH}${LOG_FILE_NAME_HEALTH}.${LOG_FILE_NAME_BACKUP}</fileNamePattern>
            <maxFileSize>${MAX_FILESIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <springProfile name="!prod">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>

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

    <logger name="${APP_CLASS_PATH}.health" level="INFO" additivity="false">
        <appender-ref ref="FILE_HEALTH"/>
        <appender-ref ref="FILE_ERROR"/>
    </logger>

</configuration>
