logbackの設定ファイルを環境毎に変える方法


基本的には、[Spring Boot logback.xmlを環境毎にを変える](http://site.oganity.pw/%E6%8A%80%E8%A1%93%E3%83%8D%E3%82%BF/spring-boot-logback-xml%E3%82%92%E7%92%B0%E5%A2%83%E6%AF%8E%E3%81%AB%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/)に記述されている通りで可能だけど、プラスして、Janinoのライブラリが必要。

 - [Janino](http://mvnrepository.com/artifact/org.codehaus.janino/janino/2.7.8)

これなしで実行すると、JaninoがないのでIfActionを無視します的なエラーメッセージがログに出力される。

ちなみに今回は、appendarをメインで定義し、loggerを環境毎に変えるようにしてみた。

```logback.xml
<configuration>
    <!-- common settings -->
    <property name="ROOT_LEVEL" value="ERROR" />
    <property name="LOG_FILE_BASE" value="application" />
    <property name="NORMAL_LOG_FILE" value="${catalina.home}/logs/${LOG_FILE_BASE}.log" />
    <property name="NORMAL_LOG_FILE_BACKUP" value="${catalina.home}/logs/${NORMAL_LOG_FILE}-%d{yyyyMMdd}" />
    <property name="ERROR_LOG_FILE" value="${catalina.home}/logs/${LOG_FILE_BASE}_error.log" />
    <property name="ERROR_LOG_FILE_BACKUP" value="${catalina.home}/logs/${ERROR_LOG_FILE}.log-%d{yyyyMMdd}" />

    <appender name="NORMAL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <file>${NORMAL_LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${NORMAL_LOG_FILE_BACKUP}</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[%d{yyyy-MMM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{1} \(%M:%line\)] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <file>${ERROR_LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${ERROR_LOG_FILE_BACKUP}</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[%d{yyyy-MMM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{1} \(%M:%line\)] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="ASYNC_NORMAL" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="NORMAL"/>
        <includeCallerData>true</includeCallerData>
    </appender>

    <appender name="ASYNC_ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="ERROR"/>
        <includeCallerData>true</includeCallerData>
    </appender>

    <!-- include environment settings -->
    <if condition='isDefined("spring.profile.active")'>
        <then>
            <include resource="env/${spring.profile.active}/logback-logger.xml" />
        </then>
        <else>
            <include resource="logback-logger-default.xml" />
        </else>
    </if>
</configuration>
```

```logback-logger-default.xml
<included>
    <!-- Framework log level -->
    <logger name="org.springframework" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="org.thymeleaf" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="org.hibernate" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="jdbc.sqlonly" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <!-- Application log level -->
    <logger name="jp.ijufumi.sample.app" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <!-- Root log level -->
    <root level="${ROOT_LEVEL}">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </root>
</included>
```

```env/dev/logback.xml
<included>
    <!-- Framework log level -->
    <logger name="org.springframework" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="org.thymeleaf" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="org.hibernate" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <logger name="jdbc.sqlonly" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <!-- Application log level -->
    <logger name="jp.ijufumi.sample.app" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </logger>

    <!-- Root log level -->
    <root level="${ROOT_LEVEL}">
        <appender-ref ref="ASYNC_NORMAL"/>
        <appender-ref ref="ASYNC_ERROR"/>
    </root>
</included>
```