Help us understand the problem. What is going on with this article?

Spring Boot で log4jdbc-log4j2 を使用してSQLを出力する

More than 5 years have passed since last update.

Spring Boot で log4jdbc-log4j2 を使用してSQLを出力するための手順です。開発時に実行されているSQLを確認したい時に使えると思います。

http://code.google.com/p/log4jdbc-log4j2/ を参照しました。

pom.xml に log4jdbc-log4j2-jdbc4.1 を追加します。

pom.xml
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2</groupId>
            <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
            <version>1.16</version>
        </dependency>

application.properties の設定を変更します。urlのjdbcの後に :log4jdbc を追加し、driverClassName を net.sf.log4jdbc.sql.jdbcapi.DriverSpy に変更します。

(変更前)application.properties
spring.datasource.url             = jdbc:mysql://localhost/world
spring.datasource.driverClassName = com.mysql.jdbc.Driver
(変更後)application.properties
spring.datasource.url             = jdbc:log4jdbc:mysql://localhost/world
spring.datasource.driverClassName = net.sf.log4jdbc.sql.jdbcapi.DriverSpy

/resources の下に log4jdbc.log4j2.properties を作成します。

log4jdbc.log4j2.properties
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

/resources の下に logback.xml を作成します。

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

    <!-- log4jdbc-log4j2 -->
    <logger name="jdbc.sqlonly"        level="DEBUG"/>
    <logger name="jdbc.sqltiming"      level="INFO"/>
    <logger name="jdbc.audit"          level="INFO"/>
    <logger name="jdbc.resultset"      level="ERROR"/>
    <logger name="jdbc.resultsettable" level="ERROR"/>
    <logger name="jdbc.connection"     level="DEBUG"/>
</configuration>

以下の Java Configuration のクラスを作成します。

@Configuration
public class ApplicationConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

}

Spring Data JPA でも MyBatis-Spring でもSQLが出力されます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした