Spring Boot 1.4.x の Web アプリを 1.5.x へバージョンアップする ( その8 )( logback-develop.xml, logback-unittest.xml, logback-product.xml の設定を logback-spring.xml と application.properties に移動してファイルを削除する )
概要
- 今回の手順で確認できるのは以下の内容です。
- 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 へ移動してファイルを削除することにします。
参照したサイト・書籍
Spring Boot Reference Guide - 76. Logging
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.htmlSpring Bootのログ出力(概要)
http://qiita.com/NagaokaKenichi/items/7cf2f427d88dfd4f56a8
目次
手順
logback-develop.xml, logback-unittest.xml, logback-product.xml の設定を application.properties, logback-spring.xml に移動して削除する
src/main/resources の下の application.properties, application-develop.properties, application-unittest.properties を リンク先の内容 に変更します。
src/main/resources の下の logback-develop.xml, logback-unittest.xml, logback-product.xml を削除します。
clean タスク実行 → Rebuild Project → build タスクを実行して、"BUILD SUCCESSFUL" が出力されることを確認します。
bootRun を実行して Tomcat を起動してみると、コンソールにログが出力されることが確認できます。
build タスクを実行して作成された ksbysample-webapp-lending-1.5.3-RELEASE.jar を C:\webapps\ksbysample-webapp-lending\lib の下にコピーし、C:\webapps\ksbysample-webapp-lending\bat\webapp_startup.bat を修正してから起動してみます。
バナーの後には何も出力されていません。
C:\webapps\ksbysample-webapp-lending\logs の下には ksbysample-webapp-lending.log が作成されており、ログファイルにはログが出力されていることが確認できます。
ソースコード
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" && "${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
初版発行。