Fork me on GitHub
#sql
<
2019-11-06
>
valerauko10:11:19

@seancorfield what's the recommended way to log next.jdbc queries (in dev/debug)? i'd want to log every single query, one log line per query.

dharrigan10:11:43

Personally, I do this, it works for me 🙂

dharrigan10:11:47

(defn select
  [sql]
  (log/debugf "Executing JDBC [%s]." sql)
  (try
   (let [results (jdbc/execute-one! @datasource sql {:builder-fn rs/as-unqualified-lower-maps})]
     (when (seq results)
       (log/debugf "JDBC Results [%s]." results))
     results)
   (catch Exception e (log/error e))))

dharrigan10:11:01

where log is clojure.tools.logging

valerauko10:11:26

it'd be nice if i didn't have to write this (and also query time measurement) myself

jumar11:11:33

I haven't used it with clojure but you could take a look at https://github.com/p6spy/p6spy

jumar11:11:59

Some jdbc drivers may also support logging via system props/config (see https://stackoverflow.com/questions/27060563/enable-logging-for-sql-statements-when-using-jdbc)

wotbrew12:11:00

If you are happy to do a bit of hacking you can write a general facility that wraps jdbc queries using var rebinding, with-redefs or alter-var-root

wotbrew12:11:14

I think if its just for testing / debugging that sort of approach is fine. you can write a (dev/sql-logging true) style function that toggles on/off without having to change any of your 'real' code.

wotbrew13:11:45

I have also used driver level query logging as well and that can work well, but typically you will need to reprovision your connection pool or datasource so may be less handy at the REPL.

valerauko14:11:20

i've tried that too, but sadly the driver-level logging for mysql isn't exactly smart so i was looking for alternatives

curtis.summers16:11:35

In development, I just turn on SQL statement logging on the database server itself.

valerauko03:11:34

sadly the mysql general log doesn't have query execution times

valerauko03:11:17

iirc that doesn't accept a lower threshold of 0 but i'll check

valerauko05:11:52

i can't seem to get the jdbc driver to log slow queries... with hikari-cp and

:log-slow-queries  true
                    :slow-query-threshold-millis 1
(i'm testing it with select sleep(1))

seancorfield16:11:12

Given that results can be arbitrarily large, logging the entire result set seems very problematic. Logging the SQL and parameters would be OK but what about sensitive data ending up in your log files?