Skip to content
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

[GH-1418] sourceLocation null fix #487

Merged
merged 3 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 105 additions & 88 deletions api/src/wfl/jdbc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,120 +22,135 @@
(defmacro query
"Logged alias for [[clojure.java.jdbc/query]]"
([db sql-params]
`(let [db# ~db
sql-params# ~sql-params]
(logger/debug (str/join " " ["jdbc/query:" (format-db db#) sql-params#]))
(jdbc/query db# sql-params#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
sql-params# ~sql-params]
(logger/debug (str/join " " ["jdbc/query:" (format-db db#) sql-params#]) ~line)
(jdbc/query db# sql-params#))))
([db sql-params opts]
`(let [db# ~db
sql-params# ~sql-params
opts# ~opts]
(logger/debug (str/join " " ["jdbc/query:" (format-db db#) sql-params# opts#]))
(jdbc/query db# sql-params# opts#))))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
sql-params# ~sql-params
opts# ~opts]
(logger/debug (str/join " " ["jdbc/query:" (format-db db#) sql-params# opts#]) ~line)
(jdbc/query db# sql-params# opts#)))))

(defmacro update!
"Logged alias for [[clojure.java.jdbc/update!]]"
([db table set-map where-clause]
`(let [db# ~db
table# ~table
set-map# ~set-map
where-clause# ~where-clause]
(logger/info (str/join " " ["jdbc/update!" (format-db db#) table# set-map# where-clause#]))
(jdbc/update! db# table# set-map# where-clause#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
set-map# ~set-map
where-clause# ~where-clause]
(logger/info (str/join " " ["jdbc/update!" (format-db db#) table# set-map# where-clause#]) ~line)
(jdbc/update! db# table# set-map# where-clause#))))
([db table set-map where-clause opts]
`(let [db# ~db
table# ~table
set-map# ~set-map
where-clause# ~where-clause
opts# ~opts]
(logger/info (str/join " " ["jdbc/update!" (format-db db#) table# set-map# where-clause# opts#]))
(jdbc/update! db# table# set-map# where-clause# opts#))))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
set-map# ~set-map
where-clause# ~where-clause
opts# ~opts]
(logger/info (str/join " " ["jdbc/update!" (format-db db#) table# set-map# where-clause# opts#]) ~line)
(jdbc/update! db# table# set-map# where-clause# opts#)))))

(defmacro insert-multi!
"Logged alias for [[clojure.java.jdbc/insert-multi!]]"
([db table rows]
`(let [db# ~db
table# ~table
rows# ~rows]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# rows#]))
(jdbc/insert-multi! db# table# rows#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
rows# ~rows]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# rows#]) ~line)
(jdbc/insert-multi! db# table# rows#))))
([db table cols-or-rows values-or-opts]
`(let [db# ~db
table# ~table
cols-or-rows# ~cols-or-rows
values-or-opts# ~values-or-opts]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# cols-or-rows# values-or-opts#]))
(jdbc/insert-multi! db# table# cols-or-rows# values-or-opts#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
cols-or-rows# ~cols-or-rows
values-or-opts# ~values-or-opts]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# cols-or-rows# values-or-opts#]) ~line)
(jdbc/insert-multi! db# table# cols-or-rows# values-or-opts#))))
([db table cols values opts]
`(let [db# ~db
table# ~table
cols# ~cols
values# ~values
opts# ~opts]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# cols# values# opts#]))
(jdbc/insert-multi! db# table# cols# values# opts#))))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
cols# ~cols
values# ~values
opts# ~opts]
(logger/info (str/join " " ["jdbc/insert-multi!" (format-db db#) table# cols# values# opts#]) ~line)
(jdbc/insert-multi! db# table# cols# values# opts#)))))

(defmacro execute!
"Logged alias for [[clojure.java.jdbc/execute!]]"
([db sql-params]
`(let [db# ~db
sql-params# ~sql-params]
(logger/info (str/join " " ["jdbc/execute!" (format-db db#) sql-params#]))
(jdbc/execute! db# sql-params#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
sql-params# ~sql-params]
(logger/info (str/join " " ["jdbc/execute!" (format-db db#) sql-params#]) ~line)
(jdbc/execute! db# sql-params#))))
([db sql-params opts]
`(let [db# ~db
sql-params# ~sql-params
opts# ~opts]
(logger/info (str/join " " ["jdbc/execute!" (format-db db#) sql-params# opts#]))
(jdbc/execute! db# sql-params# opts#))))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
sql-params# ~sql-params
opts# ~opts]
(logger/info (str/join " " ["jdbc/execute!" (format-db db#) sql-params# opts#]) ~line)
(jdbc/execute! db# sql-params# opts#)))))

(defmacro db-do-commands
"Logged alias for [[clojure.java.jdbc/db-do-commands]]"
([db sql-commands]
`(let [db# ~db
sql-commands# ~sql-commands]
(logger/info (str/join " " ["jbs/db-do-commands" (format-db db#) sql-commands#]))
(jdbc/db-do-commands db# sql-commands#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
sql-commands# ~sql-commands]
(logger/info (str/join " " ["jbs/db-do-commands" (format-db db#) sql-commands#]) ~line)
(jdbc/db-do-commands db# sql-commands#))))
([db transaction? sql-commands]
`(let [db# ~db
transaction?# ~transaction?
sql-commands# ~sql-commands]
(logger/info (str/join " " ["jbs/db-do-commands" (format-db db#) transaction?# sql-commands#]))
(jdbc/db-do-commands db# transaction?# sql-commands#))))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
transaction?# ~transaction?
sql-commands# ~sql-commands]
(logger/info (str/join " " ["jbs/db-do-commands" (format-db db#) transaction?# sql-commands#]) ~line)
(jdbc/db-do-commands db# transaction?# sql-commands#)))))

(defmacro insert!
"Logged alias for [[clojure.java.jdbc/insert!]]"
([db table row]
`(let [db# ~db
table# ~table
row# ~row]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# row#]))
(jdbc/insert! db# table# row#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
row# ~row]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# row#]) ~line)
(jdbc/insert! db# table# row#))))
([db table cols-or-row values-or-opts]
`(let [db# ~db
table# ~table
cols-or-row# ~cols-or-row
values-or-opts# ~values-or-opts]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# cols-or-row# values-or-opts#]))
(jdbc/insert! db# table# cols-or-row# values-or-opts#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
cols-or-row# ~cols-or-row
values-or-opts# ~values-or-opts]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# cols-or-row# values-or-opts#]) ~line)
(jdbc/insert! db# table# cols-or-row# values-or-opts#))))
([db table cols values opts]
`(let [db# ~db
table# ~table
cols# ~cols
values# ~values
opts# ~opts]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# cols# values# opts#]))
"jdbc/insert" db# table# cols# values# opts#)))
(let [{:keys [line]} (meta &form)]
`(let [db# ~db
table# ~table
cols# ~cols
values# ~values
opts# ~opts]
(logger/info (str/join " " ["jdbc/insert" (format-db db#) table# cols# values# opts#]) ~line)
"jdbc/insert" db# table# cols# values# opts#))))

(defmacro with-db-transaction
"Logged alias for [[clojure.java.jdbc/with-db-transaction]]"
[binding & body]
`(let [id# (rand-int 10000)
init# ~(second binding)]
(logger/info (str/join " " ["JDBC transaction" id# "started to" (format-db init#)]))
(let [exe# (jdbc/with-db-transaction [~(first binding) init#] ~@body)]
(logger/info (str/join " " ["JDBC SQL transaction" id# "ended"]))
exe#)))
(let [{:keys [line]} (meta &form)]
`(let [id# (rand-int 10000)
init# ~(second binding)]
(logger/info (str/join " " ["JDBC transaction" id# "started to" (format-db init#)]) ~line)
(let [exe# (jdbc/with-db-transaction [~(first binding) init#] ~@body)]
(logger/info (str/join " " ["JDBC SQL transaction" id# "ended"]) ~line)
exe#))))

(defmacro prepare-statement
"Alias for [[clojure.java.jdbc/prepare-statement]].
Expand All @@ -148,13 +163,15 @@
(defmacro get-connection
"Logged alias for [[clojure.java.jdbc/prepare-statement]]"
([db-spec]
`(do
(logger/info (str/join " " ["JBDC SQL connection made (no opts):" (format-db ~db-spec)]))
(jdbc/get-connection ~db-spec)))
(let [{:keys [line]} (meta &form)]
`(do
(logger/info (str/join " " ["JBDC SQL connection made (no opts):" (format-db ~db-spec)]) ~line)
(jdbc/get-connection ~db-spec))))
([db-spec opts]
`(do
(logger/info (str/join " " ["JBDC SQL connection made:" (format-db ~db-spec) ~opts]))
(jdbc/get-connection ~db-spec ~opts))))
(let [{:keys [line]} (meta &form)]
`(do
(logger/info (str/join " " ["JBDC SQL connection made:" (format-db ~db-spec) ~opts]) ~line)
(jdbc/get-connection ~db-spec ~opts)))))

;; Expertly copied and pasted from Stack Overflow:
;; https://stackoverflow.com/a/25786990
Expand Down
30 changes: 20 additions & 10 deletions api/src/wfl/log.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,40 @@
(defmacro debug
"Log `expression` for debugging.
This is for debug or trace information."
[expression]
`(log :debug ~expression))
([expression]
(let [{:keys [line]} (meta &form)] `(debug ~expression ~line)))
([expression line]
`(log :debug ~expression :logging.googleapis.com/sourceLocation {:file ~*file* :line ~line})))

(defmacro info
"Log `expression` as information.
Used for routine information, such as ongoing status or performance."
[expression]
`(log :info ~expression))
([expression]
(let [{:keys [line]} (meta &form)] `(info ~expression ~line)))
([expression line]
`(log :info ~expression :logging.googleapis.com/sourceLocation {:file ~*file* :line ~line})))

(defmacro notice
"Log `expression` as a notice.
Used for normal but significant events, such as start up,
shut down, or a configuration change."
[expression]
`(log :notice ~expression))
([expression]
(let [{:keys [line]} (meta &form)] `(notice ~expression ~line)))
([expression line]
`(log :notice ~expression :logging.googleapis.com/sourceLocation {:file ~*file* :line ~line})))

(defmacro warn
"Log `expression` as a warning.
Used for warning events, which might cause problems."
[expression]
`(log :warning ~expression))
([expression]
(let [{:keys [line]} (meta &form)] `(warn ~expression ~line)))
([expression line]
`(log :warning ~expression :logging.googleapis.com/sourceLocation {:file ~*file* :line ~line})))

(defmacro error
"Log `expression` as an error.
Used for events that are likely to cause problems."
[expression]
`(log :error ~expression))
([expression]
(let [{:keys [line]} (meta &form)] `(error ~expression ~line)))
([expression line]
`(log :error ~expression :logging.googleapis.com/sourceLocation {:file ~*file* :line ~line})))