Showing posts with label sql logging. Show all posts
Showing posts with label sql logging. Show all posts

Friday, 23 October 2020

JDBI 3 - How to enable sql logging

 

Sometimes we would require to enable printing of sqls along with the execution time while debugging the issues or doing psr testing. 

Following are the steps to enable sql logging with time taken to execute in JDBI3:

1. Add the following code on injected Jdbi managed object for enabling sql logging:

if (config.isEnableSqlLogging()) {
SqlLogger sqlLogger = new SqlLogger() {
@Override
public void logAfterExecution(StatementContext context) {
log.info("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(),
context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS));
}
};
jdbi.setSqlLogger(sqlLogger);
}

2. Add a config parameter (optional) to control enabling of sql logging as per requirement in various environments:

enableSqlLogging : false