This project was created as a personal database logging experiment.
There are much better pre-existing solutions that can do the same (or similar)
JDBC-Logger is a alternative module for logging JDBC SQL statements in an application. Will print out 'real-looking' SQL statements.
Motivated by the fact that: SQL Statement logs with silly Question Marks (?'s) drive me bonkers!.
1 JDBC-Logger (THIS PROJECT)
insert into employee (active, first_name, hire_date, last_name, level, notes, salary, id)
values (1, 'George', '2021-11-07 02:22:04', 'Washington', 9, null, 55123.43, 1)
2 Existing Hibernate logging (DEBUG)
insert into employee (active, first_name, hire_date, last_name, level, notes, salary, id)
values (?, ?, ?, ?, ?, ?, ?, ?)
3 Existing Hibernate logging (TRACE)
insert into employee (active, first_name, hire_date, last_name, level, notes, salary, id)
values (?, ?, ?, ?, ?, ?, ?, ?)
binding parameter [1] as [BOOLEAN] - [true]
binding parameter [2] as [VARCHAR] - [George]
binding parameter [3] as [TIMESTAMP] - [Sat Nov 06 19:22:04 PDT 2021]
binding parameter [4] as [VARCHAR] - [Washington]
binding parameter [5] as [INTEGER] - [9]
binding parameter [6] as [VARCHAR] - [null]
binding parameter [7] as [DOUBLE] - [55123.43]
binding parameter [8] as [BIGINT] - [1]
DataSource dataSource = new LoggingDataSource(__ORIGINAL_DATA_SOURCE__, __YOUR_LOGGER_HERE__);
@Configuration
public class AppConfig {
@Autowired
private DataSourceProperties dataSourceProperties;
@Primary
@Bean
@ConfigurationProperties(prefix = "datasource")
public DataSource dataSource()
{
DataSource innerDataSource = dataSourceProperties.initializeDataSourceBuilder().build();
return new LoggingDataSource(innerDataSource, __YOUR_LOGGER_HERE__);
}
}
Connection innerConn = DriverManager.getConnection("jdbc:hsqldb:mem:sampleDB", "SA", "");
Connection loggingConn = new LoggingConnection(innerConn, __YOUR_LOGGER_HERE__)
// builder with 2 logging listeners```
DbLoggingBuilder.builder(new Slf4jLoggingListener(logger), new CustomLoggingListener())
// attempt to log 'actual' string value for any CLOB objects
.setClobParamLogging(true)
// date string based on PST timezone (instead of default UTC)
.setZone("PST")
// create LoggingDataSource (wrapping the original dataSource)
.createFrom(innerDataSource);
...
public class CustomLoggingListener implements LoggingListener {
@Override
public void log(String sql) {
// special logger handling here
}
}
- The SQL statement is logged immediately BEFORE the SQL is actually executed (so it gets logged even if there was a SQL Exception)
- All dates are logged using the UTC timezone by default.
- Project still needs javadocs and Readme updates.
- Still making code tweaks sporadically.
- CallableStatement SQL param logging when setting parameter by NAME
- PreparedStatement
setArray
method
- Most 'happy path' cases,
- Testing with "HSQLDB" driver
- Non-HsqlDb drivers
- No Load/Performance testing was conducted.
- This does NOT check for any Sql-Injection vulnerabilities (i.e. Log4J)
- Take a look at the nested Demo project and unittests for other usages