かんがるーさんの日記

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

Spring Boot 1.3.x の Web アプリを 1.4.x へバージョンアップする ( その21 )( Log4jdbc Spring Boot Starter を入れてみる )

概要

記事一覧はこちらです。

Spring Boot 1.3.x の Web アプリを 1.4.x へバージョンアップする ( その20 )( 気になった点を修正する ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • log4jdbc を適用するのに Log4jdbc Spring Boot Starter というライブラリが出ていたので、それに乗り換えてみます。

参照したサイト・書籍

  1. Log4jdbc Spring Boot Starter
    https://github.com/candrews/log4jdbc-spring-boot-starter

  2. How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?
    http://stackoverflow.com/questions/26698071/how-to-exclude-autoconfiguration-classes-in-spring-boot-junit-tests

目次

  1. これまでの log4jdbc を適用する手順とは
  2. Log4jdbc Spring Boot Starter の場合にはどう変わるのか
  3. 実際に適用してみる
  4. develop プロファイルで起動してログが出力されることを確認する
  5. spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を設定すればログが出力されないことを確認する
  6. 感想

手順

これまでの log4jdbc を適用する手順とは

これまで Project に log4jdbc を適用するには、以下の手順で設定していました。

  1. build.gradle に compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") を記述する。
  2. src/main/resources の下に log4jdbc.log4j2.properties を作成する。
  3. src/main/resources の下の application-develop.properties の spring.datasource.url に記述する URL に :log4jdbc を追加し、spring.datasource.tomcat.driverClassName には net.sf.log4jdbc.sql.jdbcapi.DriverSpy を記述する。
  4. src/main/resources の下の logback-develop.xml<logger name="jdbc.~" level="..."/> を記述する。

Log4jdbc Spring Boot Starter の場合にはどう変わるのか

これが Log4jdbc Spring Boot Starter を適用する場合には、以下の手順で設定すればよいだけになります。

  1. build.gradle に compile("com.integralblue:log4jdbc-spring-boot-starter:1.0.0") を記述する。
  2. src/main/resources の下の logback-develop.xml<logger name="jdbc.~" level="..."/> を記述する。
  3. log4jdbc を適用したくない profile がある場合には、src/main/resources の下の application-[profile名].properties に spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を記述する。

実際に適用してみる

  1. build.gradle を リンク先の内容 に変更します。

  2. 変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。

    Project Tool Window を見ると適用される log4jdbc のライブラリは以前と同じ org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16 のようです。

    f:id:ksby:20170415083650p:plain

  3. src/main/resources/log4jdbc.log4j2.properties を削除します。

  4. src/main/resources の下の application.properties を リンク先の内容 に変更します。

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

  6. clean タスク → Rebuild Project → build タスクを実行して “BUILD SUCCESSFUL” が表示されることを確認します。

    f:id:ksby:20170415143716p:plain

  7. Project Tool Window の src/test から「Run ‘All Tests’ with Coverage」を実行してテストが全て成功することを確認します。

    f:id:ksby:20170415144105p:plain

develop プロファイルで起動してログが出力されることを確認する

bootRun で起動して、ログイン画面から tanaka.taro@sample.com でログインした後、検索対象図書館登録画面からアジア・アフリカ図書館を選択してみます。

f:id:ksby:20170415145322p:plain

Log4jdbc Spring Boot Starter に入れ替える前と同様に JDBC のログが出力されていることが確認できます。

f:id:ksby:20170415145628p:plain

Ctrl+F2 を押して Tomcat を停止します。

spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を設定すればログが出力されないことを確認する

application-develop.properties に spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を追加します。

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

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

spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration

再度 bootRun で起動して、ログイン画面から tanaka.taro@sample.com でログインした後、検索対象図書館登録画面から米国大使館レファレンス資料室を選択してみます。

f:id:ksby:20170415152958p:plain

今度は JDBC のログは出力されませんでした。

f:id:ksby:20170415153304p:plain

Ctrl+F2 を押して Tomcat を停止します。application-develop.properties も元に戻します。

感想

これまで log4jdbc を適用するにはいろんな箇所を設定しないといけなかったのが省略されるので、楽ですね。今後はこれを使っていきたいと思います。

ソースコード

build.gradle

dependencies {
    ..........

    // dependency-management-plugin によりバージョン番号が自動で設定されないもの、あるいは最新バージョンを指定したいもの
    runtime("${jdbcDriver}")
    compile("com.integralblue:log4jdbc-spring-boot-starter:1.0.0")
    compile("org.simpleframework:simple-xml:2.7.1")
  • compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") を削除します。
  • compile("com.integralblue:log4jdbc-spring-boot-starter:1.0.0") を追加します。

application.properties

doma.dialect=org.seasar.doma.jdbc.dialect.PostgresDialect

spring.datasource.tomcat.url=jdbc:postgresql://localhost/ksbylending
spring.datasource.tomcat.username=ksbylending_user
spring.datasource.tomcat.password=xxxxxxxx
spring.datasource.tomcat.driverClassName=org.postgresql.Driver

spring.session.store-type=redis

spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.enabled=false
spring.freemarker.prefer-file-system-access=false
  • application-product.properties に記載してある以下の設定をコピーします。
    • spring.datasource.tomcat.url=jdbc:postgresql://localhost/ksbylending
    • spring.datasource.tomcat.username=ksbylending_user
    • spring.datasource.tomcat.password=xxxxxxxx
    • spring.datasource.tomcat.driverClassName=org.postgresql.Driver

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

■application-develop.properties

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383
  • spring.datasource.tomcat.url, spring.datasource.tomcat.username, spring.datasource.tomcat.password, spring.datasource.tomcat.driverClassName の設定を削除します。

■application-product.properties

server.tomcat.basedir=C:/webapps/ksbysample-webapp-lending
logging.file=${server.tomcat.basedir}/logs/ksbysample-webapp-lending.log

spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383
  • spring.datasource.tomcat.url, spring.datasource.tomcat.username, spring.datasource.tomcat.password, spring.datasource.tomcat.driverClassName の設定を削除します。
  • spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を追加します。

■application-unittest.properties

spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383
  • spring.datasource.tomcat.url, spring.datasource.tomcat.username, spring.datasource.tomcat.password, spring.datasource.tomcat.driverClassName の設定を削除します。
  • spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration を追加します。

履歴

2017/04/15
初版発行。