M
M
mikhailro2018-04-30 11:33:57
Java
mikhailro, 2018-04-30 11:33:57

How to implement JDBC logging?

Good afternoon!
I need to implement logging of sql queries in a java application.
The project uses JDBC, JDK8
I wanted to use log4jdbc, but, as I understand it, it supports JDK 1.7 at most.
It is important that logging be automatic, without the need to write anything when calling the request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vorh, 2018-04-30
@mikhailro

You can use datasource-proxy
It allows you to log all requests, and if it is not necessary, configure logging only for "heavy requests" that run more than n seconds in time.
The log entry 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 found here
Simple configuration example :
DataSource db = ProxyDataSourceBuilder
            .create(actualDataSource)
            .logQueryByCommons(INFO)    // or logQueryBySlf4j(INFO)
            .build();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question