loggingclojurehugsql

Log sql statments/queries executed by HugSQL


I want to log all SQL strings executed by HugSQL. I looked through the docs, but couldn't find anything. Whats the recommended way?


Solution

  • I solved it myself by digging around the hugsql source. It works similar to converting the result set of a generated function (https://github.com/layerware/hugsql/issues/21):

    (defn log-sqlvec [sqlvec]
      (log/info (->> sqlvec
                  (map #(clojure.string/replace (or % "") #"\n" ""))
                  (clojure.string/join " ; "))))
    
    (defn log-command-fn [this db sqlvec options]
      (log-sqlvec sqlvec)
      (condp contains? (:command options)
        #{:!} (hugsql.adapter/execute this db sqlvec options)
        #{:? :<!} (hugsql.adapter/query this db sqlvec options)))
    
    (defmethod hugsql.core/hugsql-command-fn :! [sym] `log-command-fn)
    (defmethod hugsql.core/hugsql-command-fn :<! [sym] `log-command-fn)
    (defmethod hugsql.core/hugsql-command-fn :? [sym] `log-command-fn)