<configuration>
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

    <springProperty scope="context" name="LOKI_PUSH_URL" source="loki.url"/>
    <springProperty scope="context" name="LOGGING_PARENT" source="logging.parent"/>
    <springProperty scope="context" name="APP_NAME" source="spring.application.name"/>

    <appender name="Loki" class="com.github.loki4j.logback.Loki4jAppender">
        <metricsEnabled>true</metricsEnabled>
        <http class="com.github.loki4j.logback.ApacheHttpSender">
            <url>${LOKI_PUSH_URL:-http://localhost/loki/api/v1/push}</url>
        </http>
        <format>
            <label>
                <pattern>app=${APP_NAME:-none},host=${HOSTNAME:-none},level=%level</pattern>
                <readMarkers>true</readMarkers>
            </label>
            <message>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [${HOSTNAME}] [%t] %logger #@# %m%n%wEx</pattern>
            </message>
            <sortByTime>true</sortByTime>
        </format>
    </appender>

    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%5p) %clr([${HOSTNAME}]){yellow} %clr([%t]){magenta} %clr(%logger{40}){cyan} #@# %m%n%wEx</pattern>
        </encoder>
    </appender>

    <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGGING_PARENT:-.}/${APP_NAME:-run}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGGING_PARENT:-.}/archive/${APP_NAME:-run}-%d{yyyy-MM-dd}.gz</fileNamePattern>
            <MaxHistory>7</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [${HOSTNAME}] [%t] %logger #@# %m%n%wEx</pattern>
        </encoder>
    </appender>

    <logger name="com.zaxxer.hikari" level="ERROR"/>
    <logger name="com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver" level="WARN"/>

    <root level="INFO">
        <appender-ref ref="Loki"/>
        <appender-ref ref="Console"/>
        <appender-ref ref="RollingFile"/>
    </root>
</configuration>