Marcin Wiśniewski
Marcin Wiśniewski

Reputation: 253

Logging SQL statements in Spring Boot + jdbi

I'm using datasource with DBI:

@Autowired
DataSource dataSource;

@Bean
public DBI dbiBean() {
    DBI dbi = new DBI(dataSource);
    return dbi;
}

I have no idea how to configure logging framework to log SQL statements. I tried:

logging:
 level: 
   org.hibernate: TRACE
   org.skife.jdbi: TRACE
   java.sql: TRACE

but it doesn't work.

Upvotes: 3

Views: 6015

Answers (2)

ouid
ouid

Reputation: 433

Starting from JDBI version 3.2.0, SqlLogger interface should be used:

  Jdbi jdbi;
  jdbi.setSqlLogger(new Slf4JSqlLogger());

As documentation says, Slf4JSqlLogger is a simple SqlLogger that emits some diagnostic information about Jdbi usage. You can provide existing instance of Slf4J Logger class to the Slf4JSqlLogger constructor then the provided logger will be used to log SQL statements, otherwise, if no-args constructor is used, a logger called org.jdbi.sql will be created and used.

SQL statements are logged with debug log level, so in order to see them it might be necessary to set the root level of the logging framework to debug.

Upvotes: 6

Paul Vickery
Paul Vickery

Reputation: 31

Add the line to set the SQL log like this. Use either SLF4JLog or another logger there.

@Bean
public DBI dbiBean() {
    DBI dbi = new DBI(dataSource);
    dbi.setSQLLog(new SLF4JLog());
    return dbi;
}

Upvotes: 3

Related Questions