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 というライブラリが出ていたので、それに乗り換えてみます。
参照したサイト・書籍
Log4jdbc Spring Boot Starter
https://github.com/candrews/log4jdbc-spring-boot-starterHow to exclude *AutoConfiguration classes in Spring Boot JUnit tests?
http://stackoverflow.com/questions/26698071/how-to-exclude-autoconfiguration-classes-in-spring-boot-junit-tests
目次
- これまでの log4jdbc を適用する手順とは
- Log4jdbc Spring Boot Starter の場合にはどう変わるのか
- 実際に適用してみる
- develop プロファイルで起動してログが出力されることを確認する
spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration
を設定すればログが出力されないことを確認する- 感想
手順
これまでの log4jdbc を適用する手順とは
これまで Project に log4jdbc を適用するには、以下の手順で設定していました。
- build.gradle に
compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16")
を記述する。 - src/main/resources の下に log4jdbc.log4j2.properties を作成する。
- src/main/resources の下の application-develop.properties の spring.datasource.url に記述する URL に
:log4jdbc
を追加し、spring.datasource.tomcat.driverClassName
にはnet.sf.log4jdbc.sql.jdbcapi.DriverSpy
を記述する。 - src/main/resources の下の logback-develop.xml に
<logger name="jdbc.~" level="..."/>
を記述する。
Log4jdbc Spring Boot Starter の場合にはどう変わるのか
これが Log4jdbc Spring Boot Starter を適用する場合には、以下の手順で設定すればよいだけになります。
- build.gradle に
compile("com.integralblue:log4jdbc-spring-boot-starter:1.0.0")
を記述する。 - src/main/resources の下の logback-develop.xml に
<logger name="jdbc.~" level="..."/>
を記述する。 - log4jdbc を適用したくない profile がある場合には、src/main/resources の下の application-[profile名].properties に
spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration
を記述する。
実際に適用してみる
build.gradle を リンク先の内容 に変更します。
変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。
Project Tool Window を見ると適用される log4jdbc のライブラリは以前と同じ
org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16
のようです。src/main/resources/log4jdbc.log4j2.properties を削除します。
src/main/resources の下の application.properties を リンク先の内容 に変更します。
src/main/resources の下の application-develop.properties, application-product.properties, application-unittest.properties を リンク先の内容 に変更します。
clean タスク → Rebuild Project → build タスクを実行して “BUILD SUCCESSFUL” が表示されることを確認します。
Project Tool Window の src/test から「Run ‘All Tests’ with Coverage」を実行してテストが全て成功することを確認します。
develop プロファイルで起動してログが出力されることを確認する
bootRun で起動して、ログイン画面から tanaka.taro@sample.com でログインした後、検索対象図書館登録画面からアジア・アフリカ図書館を選択してみます。
Log4jdbc Spring Boot Starter に入れ替える前と同様に JDBC のログが出力されていることが確認できます。
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 でログインした後、検索対象図書館登録画面から米国大使館レファレンス資料室を選択してみます。
今度は JDBC のログは出力されませんでした。
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
初版発行。