How do I log SQL Statements in Dropwizard
Asked Answered
H

3

13

How do I turn on SQL query logging for a Dropwizard application? I would like it to only log SQL in certain environments.

Honorific answered 30/3, 2015 at 18:18 Comment(0)
H
23

In your application YAML file add a "logging:" definition like the following:

# Logging settings.
logging:

  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
  level: INFO

   # Logger-specific levels.
   loggers:

      # Overrides the levels of certain packages or files.
      "org.skife.jdbi.v2": TRACE 
Honorific answered 30/3, 2015 at 18:18 Comment(3)
This does not help. The SQL statement is logged but with parameters shown as simple '?' like: "SELECT * FROM XY WHERE ID = ?"Playlet
If that is what you need @heaphach, hopefully you can use a debugger to figure out what parameters are being passed in. For the question I had above, this answer is sufficient and accurate, even if it does not solve your particular issue.Honorific
Yeah for me its clear what SQL is ued, but some managers want to log the exact SQL statement. However you are right, you answered the question above.Playlet
B
2

Are you using jdbi?, if so, this is working for me:

Set a logger when you create the DBI instance:

DBI dbi = new DBI(dataSource);
dbi.setSQLLog(new SLF4JLog());

Add this configuration to your config file:

logging:
  level: INFO
  loggers:
    "org.skife": TRACE

I guess this idea should be also valid for Hibernate or any other DB access framework.

Bandaid answered 6/6, 2020 at 17:9 Comment(0)
D
0

Using jdbi 3, I had to configure the Slf4JSqlLogger like this:

jdbi.setSqlLogger(new Slf4JSqlLogger());

... And set the log level of org.jdbi.sql to TRACE in my config.yaml:

logging:
  level: INFO
  loggers:
    org.jdbi.sql: TRACE
Drank answered 5/2 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.