How to implement logging JDBC?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
17 views
Good day!

I need to implement logging sql queries in the java application.
The project uses JDBC JDK8
Wanted to use log4jdbc, but as I understand it, it max supports JDK 1.7

It is important that the logging was automatic, with no need to write anything when calling the query.
by | 17 views

1 Answer

0 like 0 dislike
You can use the datasource-proxy
It allows you to log all requests, and if it is not necessary to configure logging only for "heavy" query that run longer than n seconds in time.
The entry in the log looks like this:

Name:MyDS, Time:1, Success:True, Type:Statement, Batch:False, QuerySize:1, BatchSize:0, Query:["CREATE TABLE users (id INT, name VARCHAR(20))"], Params:[] Name:MyDS, Time:0, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:2, Query:["INSERT INTO users (id, name) VALUES (?, ?)"], Params:[(1=1,2=foo),(1=2,2=bar)] Name:MyDS, Time:1, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:2, Query:["INSERT INTO users (id, name) VALUES (?, ?)"], Params:[(1=3,2=FOO) (1=4,2=BAR)] Name:MyDS, Time:3, Success:True, Type:Statement, Batch:False, QuerySize:1, BatchSize:0, Query:["SELECT COUNT(*) FROM users"], Params:[]


Examples of configurations and settings can be viewed here

Simple example configuration :
DataSource db = ProxyDataSourceBuilder .create(actualDataSource) .logQueryByCommons(INFO) // or logQueryBySlf4j(INFO) .build();
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users