かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot 1.4.x の Web アプリを 1.5.x へバージョンアップする ( その8 )( logback-develop.xml, logback-unittest.xml, logback-product.xml の設定を logback-spring.xml と application.properties に移動してファイルを削除する )

概要

記事一覧こちらです。

Spring Boot 1.4.x の Web アプリを 1.5.x へバージョンアップする ( その7 )( Gradle を 2.13 → 3.5 へバージョンアップし、FindBugs Gradle Plugin が出力する大量のログを抑制する ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • Log4jdbc Spring Boot Starter の README.md と、そこにリンクのあった Spring Boot Reference Guide - 76. Logging を読んでいて、ログレベルを application.properties で設定できることに気づきました。古いバージョンの Spring Boot Reference Guide も読み返してみると Spring Boot 1.2 の頃から設定できていたようです。。。
    • ログレベルの設定を logback-develop.xml, logback-unittest.xml, logback-product.xml から application.properties に移動します。移動すると logback-spring.xml 以外のファイルはほとんど記述する内容がなくなるので、残りの設定を logback-spring.xml へ移動してファイルを削除することにします。

参照したサイト・書籍

  1. Spring Boot Reference Guide - 76. Logging
    https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

  2. Spring Bootのログ出力(概要)
    http://qiita.com/NagaokaKenichi/items/7cf2f427d88dfd4f56a8

目次

  1. logback-develop.xml, logback-unittest.xml, logback-product.xml の設定を application.properties, logback-spring.xml に移動して削除する

手順

logback-develop.xml, logback-unittest.xml, logback-product.xml の設定を application.properties, logback-spring.xml に移動して削除する

  1. src/main/resources の下の application.properties, application-develop.properties, application-unittest.properties を リンク先の内容 に変更します。

  2. src/main/resources の下の logback-spring.xmlリンク先の内容 に変更します。

  3. src/main/resources の下の logback-develop.xml, logback-unittest.xml, logback-product.xml を削除します。

  4. clean タスク実行 → Rebuild Project → build タスクを実行して、"BUILD SUCCESSFUL" が出力されることを確認します。

    f:id:ksby:20170607230300p:plain

  5. bootRun を実行して Tomcat を起動してみると、コンソールにログが出力されることが確認できます。

    f:id:ksby:20170608000052p:plain

  6. build タスクを実行して作成された ksbysample-webapp-lending-1.5.3-RELEASE.jar を C:\webapps\ksbysample-webapp-lending\lib の下にコピーし、C:\webapps\ksbysample-webapp-lending\bat\webapp_startup.bat を修正してから起動してみます。

    バナーの後には何も出力されていません。

    f:id:ksby:20170608000833p:plain

    C:\webapps\ksbysample-webapp-lending\logs の下には ksbysample-webapp-lending.log が作成されており、ログファイルにはログが出力されていることが確認できます。

    f:id:ksby:20170608001029p:plain

ソースコード

application.properties, application-develop.properties, application-unittest.properties

■application.properties

..........

valueshelper.classpath.prefix=

logging.level.root=INFO
logging.level.org.seasar.doma=ERROR
  • logging.level.~ の設定を追加します(2行)。

■application-develop.properties

..........

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383

# Spring MVC
logging.level.org.springframework.web=DEBUG
# log4jdbc-log4j2
logging.level.jdbc.sqlonly=DEBUG
logging.level.jdbc.sqltiming=INFO
logging.level.jdbc.audit=INFO
logging.level.jdbc.resultset=ERROR
logging.level.jdbc.resultsettable=ERROR
logging.level.jdbc.connection=DEBUG
  • logging.level.~ の設定を追加します(7行)。

■application-unittest.properties

..........

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383

logging.level.root=OFF
  • logging.level.~ の設定を追加します(1行)。

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <if condition='"${spring.profiles.active}" == "product"'>
        <then>
            <property name="LOG_FILE" value="${LOG_FILE}"/>
            <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <encoder>
                    <pattern>${FILE_LOG_PATTERN}</pattern>
                </encoder>
                <file>${LOG_FILE}</file>
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
                    <maxHistory>30</maxHistory>
                </rollingPolicy>
            </appender>
        </then>
    </if>

    <!-- Tomcat JDBC Connection Pool SlowQueryReport interceptor 用ログファイル -->
    <if condition='"${spring.profiles.active}" == "product" &amp;&amp; "${slowquery.logging.file}" != ""'>
        <then>
            <springProperty scope="context" name="slowQueryLogFile" source="slowquery.logging.file"/>
            <appender name="SLOWQUERY_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <encoder>
                    <pattern>${FILE_LOG_PATTERN}</pattern>
                </encoder>
                <file>${slowQueryLogFile}</file>
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>${slowQueryLogFile}.%d{yyyy-MM-dd}</fileNamePattern>
                    <maxHistory>30</maxHistory>
                </rollingPolicy>
            </appender>

            <logger name="org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport" level="INFO">
                <appender-ref ref="SLOWQUERY_LOG_FILE"/>
            </logger>
        </then>
    </if>

    <if condition='"${spring.profiles.active}" == "develop"'>
        <then>
            <root>
                <appender-ref ref="CONSOLE"/>
            </root>
        </then>
    </if>
    <if condition='"${spring.profiles.active}" == "product"'>
        <then>
            <root>
                <appender-ref ref="FILE"/>
            </root>
        </then>
    </if>
</configuration>
  • <appender name="CONSOLE" ...> ... </appender><include resource="org/springframework/boot/logging/logback/console-appender.xml"/> に変更します。
  • <!-- Tomcat JDBC Connection Pool SlowQueryReport interceptor 用ログファイル --> の if 文の中に <logger name="org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport" level="INFO"> ... </logger> の設定を追加します。
  • <include resource="logback-develop.xml"/><root><appender-ref ref="CONSOLE"/></root> に変更します。
  • <if condition='"${spring.profiles.active}" == "unittest"'> ... </if> を削除します。
  • <include resource="logback-product.xml"/><root><appender-ref ref="FILE"/></root> に変更します。

履歴

2017/06/08
初版発行。