-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable query logging #3
Comments
What about adding another key to the Boundary record? For example, |
Do you mean To answer my question from earlier:
What I was missing is that I'm using Although, that got me thinking, what if instead of suppressing the log entirely, you could pass it a format function that gets applied to queries before logging is done. Something like: (afterQuery [_ exec-info query-infos]
(let [elapsed (.getElapsedTime exec-info)
queries (mapv logged-query query-infos)
format-queries (or query-format-fn identity)]
(if (= (count queries) 1)
(log/log logger :info ::sql/query {:query (first (format-queries queries)), :elapsed elapsed})
(log/log logger :info ::sql/batch-query {:queries (format-queries queries), :elapsed elapsed})) As a global option (possibly alongside What do you think? |
Yep.
Yes, that's what you should be able to do. Your functions should be written around the boundary, so when you want logging you use (extend-protocol Foobar
sql/Boundary
(get-foo [{:keys [spec]} id]
(db/q spec ...))
(get-bar [{:keys [unlogged-spec]} id]
(db/q unlogged-spec ...))) |
Ah, I see, I understand now, thank you. I'll open a PR (either later tonight or tomorrow). I still like the idea of being able to format the query vectors before logging, since it means I wouldn't need to give up logging completely for a query when I know what I want to truncate or mask, but that can be something that's addressed at a later time. So maybe you can keep it in mind and think about it? (Let me know if you want a PR) Thanks for the help. I will leave this issue open until I've got a PR. |
Would the format query function just be there to format how the query appears in the logs? If so, I don't think that's a bad idea. |
Yes, I think just to preprocess/filter the query vector right before logging: (log/log logger :info ::sql/query {:query (first (format-queries queries)), :elapsed elapsed}) For example, something like: (defn my-query-formatter [query]
(into [(first query)]
(map #(if (> (count %) 50) "...truncated..." %) (rest query))))
Something like that anyway. |
I'm trying to see how to implement duct.database.sql: (defrecord Boundary [spec unlogged-spec])
(defmethod ig/init-key :duct.database/sql [_ spec]
(->Boundary spec nil)) duct.database.sql.hikaricp: (defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url] :as options}]
(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))}
{:datasource datasource}))) Or perhaps leave (defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url unlogged-spec?] :as options}]
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource))]
(cond-> (sql/->Boundary {:datasource (cond-> datasource logger (wrap-logger logger))})
unlogged-spec? (assoc :unlogged-spec {:datasource datasource})))) |
The latter option - leave You can also just add the (defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url] :as options}]
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource))]
(if logger
(-> (sql/->Boundary {:datasource (wrap-logger datasource logger})
(assoc :unlogged-spec {:datasource datasource}))
(sql/->Boundary {:datasource datasource})))) |
Hi there, I stopped by to say that the logging is useful, I would probably make the logging level configurable so that you can decide via other means if you want to show it or not...any thought? Would that be a good PR to open in addition to this one? |
Currently query logging can be turned on or off via the configuration. This (unfinished) PR is for selective logging - i.e. you may want to have logging for some queries but not for others. What scenario were you thinking wasn't covered? |
I think the ability to decide the logging level is probably what I am after. However I think I am hijacking this is issue at this point. I will open a new one and describe things better..thank you! |
I need to be able to disable the query logging on a per-query basis, as, in general, I like having the logging, but for some query I don't want logging.
The use cases are:
In both cases, only some queries are affected, so it would be good to only disable logging for those, rather than globally. Technically only the affected values need to be masked, but I don't know of any way to do that.
Looking at the code, it looks like I can disable logging globally for all queries by not passing a logger to the component, but I'm not actually sure how -- I haven't explicitly set a logger for the hikaricp component in my duct config but it still seems to get one (maybe I'm missing something), otherwise I could work around this by having two components: one with logging and one without, although then it applies to an entire transaction (plus the awkwardness of having two DB objects in components that need this).
I can open a PR, if you give me your thoughts on this. My initial thought is to introduce a dynamic var which defaults to "log enabled" that can be set, something like:
(binding [duct.database.sql.hikaricp/*logging* true] (sql-statement))
although I don't actually know how the proxy datasource works -- is afterQuery called in the same thread a the caller? Alternatively, perhaps the datasource can be temporarily unwrapped.The text was updated successfully, but these errors were encountered: