13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

基本的には、Spring Boot logback.xmlを環境毎にを変えるに記述されている通りで可能だけど、プラスして、Janinoのライブラリが必要。

これなしで実行すると、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>
13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?