If you use JDBC connection to run SQL scripts in your java project, then logging could be quite useful to monitor what was executed after all - request body, request parameters, execution time.
To see sql execution logs, all we need to do is to implement new logger class:
@Log4j2
public class JdbcLogger implements SqlLogger {
@Override
public void logAfterExecution(StatementContext context) {
log.info("Raw SQL: {}; ", context.getRawSql());
log.info("Bindings: {};", context.getBinding());
log.info("Elapsed (millisecond): {}", context.getElapsedTime(MILLIS));
}
}
And add it to our jdbi connection method:
public JdbcConnection(JdbcConfig conf) {
Jdbi jdbi = Jdbi.create(conf.getConnectionUrl());
jdbi.installPlugin(new SqlObjectPlugin());
jdbi.setSqlLogger(new JdbcLogger());
this.handle = jdbi.open();
}
And that's it. Profit👍
Now while running test cases in console output we could see:
- SQL request body
- SQL request parameters
- Execution time