Ability to format logged queries#5
Ability to format logged queries#5danielytics wants to merge 2 commits intoduct-framework:masterfrom
Conversation
|
Commit messages of this repository should follow the seven rules of a great Git commit message, as mentioned in the project's contributing guidelines. It looks like there's a few issues with the commit messages in this pull request:
|
707975c to
adb73a5
Compare
0a8c6c1 to
ffb5213
Compare
ffb5213 to
037fcb1
Compare
weavejester
left a comment
There was a problem hiding this comment.
Sorry it too so long to get round to reviewing this. There are a few changes necessary that I've commented on.
| (let [query (.getQuery query-info) | ||
| params (query-parameter-lists query-info)] | ||
| (into [query] (if (= (count params) 1) (first params) params)))) | ||
| (into [query] (if (= (count params) 1) (first params) params)))) |
There was a problem hiding this comment.
Looks like a space was accidentally deleted here, misaligning the indentation.
| (is (empty? @logs)))) | ||
|
|
||
| (defn log-query-formatter [[query & params]] | ||
| (into |
There was a problem hiding this comment.
Nonstandard indentation, and also short enough to put on one line. Could you format as:
(into [(str (re-find #"^\w+" query) "...")] params)| (deftest formatted-logging-test | ||
| (let [logs (atom []) | ||
| logger (->AtomLogger logs) | ||
| hikaricp (ig/init-key ::sql/hikaricp {:jdbc-url "jdbc:sqlite:" :logger logger :log-query-formatter log-query-formatter}) |
There was a problem hiding this comment.
Line longer than 80 characters.
| (hikari-cp/make-datasource) | ||
| (cond-> logger (wrap-logger logger)))})) | ||
| (hikari-cp/make-datasource))] | ||
| (if logger |
There was a problem hiding this comment.
Why use an if here, instead of tacking the extra assoc onto the end of the cond->?
There was a problem hiding this comment.
I think the reason was so that it only runs:
(assoc boundary :unlogged-spec {:datasource datasource})when a logger is set. Perhaps just always set unlogged-spec, regardless of if a logger is specified or not?
Something like this:
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource))]
(-> (sql/->Boundary {:datasource (cond-> datasource logger (wrap-logger logger log-query-formatter))})
(assoc :unlogged-spec {:datasource datasource})))PS: I'm not sure how you want it formatted, I'm having a hard time the wrap-logger line under 80 characters without introducing another binding in the let. Perhaps this:
(defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url log-query-formatter]
:or {log-query-formatter identity} :as options}]
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource))
logged-datasource (cond-> datasource
logger (wrap-logger logger log-query-formatter))]
(assoc (sql/->Boundary {:datasource logged-datasource})
:unlogged-spec {:datasource datasource})))There was a problem hiding this comment.
I see now. Then perhaps let's factor out some of this into a separate function for clarity:
(defn- logged-boundary [datasource {:keys [logger log-query-formatter]}]
(let [logged-source (wrap-logger datasource logger log-query-formatter)]
(-> (sql/->Boundary {:datasource logged-source})
(assoc :unlogged-spec {:datasource datasource}))))
(defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url logger] :as options}]
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource))]
(if logger
(logged-boundary data-source options)
(sql/->Boundary {:datasource datasource}))))What do you think?
There was a problem hiding this comment.
That looks good to me. I'll push this and the other changes soon.
| [_ {:keys [logger connection-uri jdbc-url] :as options}] | ||
| (sql/->Boundary {:datasource | ||
| (-> (dissoc options :logger) | ||
| [_ {:keys [logger connection-uri jdbc-url log-query-formatter] :as options}] |
There was a problem hiding this comment.
Adding a default value for log-query-formatter as identity here would prevent a need for the (or query-formatter identity) later on.
Adds an optional key
:log-query-formatterto the component, which takes a function(fn [query] ...)that gets passed each query before it gets logged, for allowing custom formatting of the logged query. This does not impact the execution of the query, only how it gets logged.As briefly discussed in #3
I'm unsure of the key name and whether it should accept a vector of all queries, or each individual query like now. Feedback welcome.