I need to log all the queries to an Oracle database in my project to a log file.
What would be a good solution to achieve this? Some sample usage would be appreciated.
I have looked at SLF4J with jdbcdslog, but I'm not sure how I can log to a file with it. Moreover, I would need to "filter" some of the logs (because I don't need to know when some getxxxx
method get's invoked)
Preferably, I'd prefer to use java.util.logging
but it is not a requirement.
Thanks.
** UPDATE **
I found this Oracle article, however it does not really tell how to programatically do the same thing.
After much reading, this is how I got things working :
NOTE : Fore more information, read the Oracle Diagnosability in JDBC document
And here's the magic :
The properties file (from Oracle's documentation) :
Basically, this is where the handlers are declared
Declares the
ConsoleHandler
to be used by Oracle's JDBC driver. Any and any number of handlers can be declared here, one per line, with the class' full qualified name :One can provide their own custom made handlers with the same rule. The following lines are to setup the handler
will call the methode
setLevel(Level.INFO)
of theConsoleHandler
handler instance.will call the method
setFoo("Bar")
of theMyHandler
handler instance. And that's it.Happy logging!
As an update to the answer of @Martin_Schröder, it also now exists log4jdbc-log4j2, which allows to use either slf4j or Log4j2, is available on the Maven repository, and supports JDBC 4.1 (Java 7).
You configure where log messages are written by configuring where the underlying logging engine is sending them. Since you're talking about slf4j, that means you've got to configure the thing that it is bridging to (the
java.util.logging
system in your case; called JUL henceforth).Unfortunately, JUL is among the more confusing systems to configure. Here's where I'll just cut to the chase. Create a file in your deployed classpath called
logging.properties
(I think; this is one of the confusing bits) which contains something like this:The documentation on the
FileHandler
class describes things that you can configure.If you are using Spring, then the datasource-proxy is very convenient. You can basically wrap around any
DataSource
and just add the logging behavior.If you're using Java EE, then P6spy is a good alternative:
Behind the scenes, P6spy provides the statement interceptor at the
Driver
level, which is much more convenient for Java EE applications because theDataSource
is provided by the application server.suggest you look at jdbcdslog's user guide and discussion group.
A quick look at the user guide suggests that you can wrap (decorate) any JDBC connection with one of jdbcdslog's special logging wrappers, and it will log to whatever place you configure.
Furthermore it says it uses slf4j which supports logging to several logging engines including java.util.logging, so what you suggest seems very possible.
(But I'm not familiar with this jdbcdslog so I'm not sure how to get things configured.)
I'm measuring the performance of my jdbc driver, this is a Tandem Non/Stop DB, and just setting the LogWriter in the DriverManager like this:
The logging on Queries started working.
Just as an update, I also found out that for some JDBC drivers the solution can NOT be accomplished programatically (by code changes). For example, I'm using a JDBC driver for Tandem t4 driver, and even though I added all what the manuals said about enabling JDBC tracing, it just worked from time to time and just for Queries..
Then, I was told to use just the following parameter (as a VM Option):
And it just simple started working..