Makes logging unified

This commit is contained in:
2023-10-30 12:35:18 -07:00
parent f0a7c378f7
commit 930b900849
44 changed files with 485 additions and 555 deletions

View File

@@ -4,7 +4,7 @@
[buddy.sign.jwt :as jwt]
[clj-http.client :as http]
[clj-time.core :as time]
[clojure.tools.logging :as log]
[auto-ap.logging :as alog]
[config.core :refer [env]]
[com.brunobonacci.mulog :as mu]
[clojure.java.io :as io]
@@ -99,7 +99,8 @@
{:status 401
:body "Couldn't authenticate"}))
(catch Exception e
(log/warn e)
(alog/warn ::cant-authenticate
:error e)
{:status 401
:body (str "Couldn't authenticate " (.toString e))})))

View File

@@ -16,7 +16,6 @@
[clj-time.core :as time]
[clojure.data.csv :as csv]
[clojure.edn :refer [read-string]]
[clojure.tools.logging :as log]
[com.unbounce.dogstatsd.core :as statsd]
[config.core :refer [env]]
[datomic.api :as dc]
@@ -277,7 +276,8 @@
(let [start-date (or (some-> (query-params "start-date")
(atime/parse atime/iso-date))
(time/plus (time/now) (time/days -120)))]
(log/info "exporting for " (query-params "client-code") "starting" start-date)
(alog/info ::exporting-ledger
:params query-params)
(assert-admin identity)
(statsd/time! [(str "export.time") {:tags #{(client-tag query-params)
"export:ledger2"}}]
@@ -458,7 +458,7 @@
(defn export-raw [{:keys [query-params identity]}]
(assert-admin identity)
(log/info "Executing raw query " (get query-params "query" ))
(alog/info ::executing-query :q (get query-params "query" ))
(statsd/time! [(str "export.time") {:tags #{"export:raw"}}]
{:body
(into (list) (apply dc/q (read-string (get query-params "query" )) (into [(dc/db conn)] (read-string (get query-params "args" "[]")))))}))

View File

@@ -4,12 +4,11 @@
[auto-ap.datomic.clients :as d-clients]
[auto-ap.graphql :as ql]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.logging :refer [warn-event]]
[auto-ap.logging :as alog]
[auto-ap.routes.utils :refer [wrap-secure]]
[buddy.auth :refer [throw-unauthorized]]
[clojure.edn :as edn]
[clojure.set :as set]
[clojure.tools.logging :as log]
[datomic.api :as dc]))
@@ -30,21 +29,21 @@
clients) ))
:headers {"Content-Type" "application/edn"}})
(catch Throwable e
(log/info "here we are " (.getCause e))
(if-let [result (:result (ex-data e))]
(do (log/warn "Graphql Result error" e)
(do (alog/warn ::result-error :error e)
{:status 400
:body (pr-str result)
:headers {"Content-Type" "application/edn"}})
(if-let [message (:validation-error (ex-data (.getCause e)) )]
(do
(warn-event "GraphQL Validation error" {:message message
:data e})
(alog/warn ::graphql-validation-error
:message message
:error e)
{:status 400
:body (pr-str {:errors [{:message message}]})
:headers {"Content-Type" "application/edn"}})
(do (log/error "GraphQL error" e)
(do (alog/error ::error :error e)
{:status 500
:body (pr-str {:errors [{:message (str "Unhandled error:" (str e))}]})
:headers {"Content-Type" "application/edn"}}))))))

View File

@@ -16,13 +16,12 @@
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.logging :as log]
[config.core :refer [env]]
[datomic.api :as dc]
[digest]
[iol-ion.tx :refer [random-tempid]]
[ring.middleware.json :refer [wrap-json-response]]
[unilog.context :as lc]
[com.brunobonacci.mulog :as mu]
[auto-ap.logging :as alog])
(:import
(java.util UUID)))
@@ -251,25 +250,24 @@
(map #(assoc % :invoice/source-url-admin-only (boolean (> client-count 1))) is)))
(defn import-uploaded-invoice [user imports]
(lc/with-context {:area "upload-invoice"}
(log/info "Number of invoices to import is" (count imports))
(let [potential-invoices (->> imports
(map import->invoice)
(map #(validate-invoice % user))
admin-only-if-multiple-clients
(mapv d-invoices/code-invoice)
(mapv (fn [i] [:propose-invoice i])))]
(log/info "creating invoice" potential-invoices)
(let [tx (audit-transact potential-invoices user)]
(when-not (seq (dc/q '[:find ?i
:in $ [?i ...]
:where [?i :invoice/invoice-number]]
(:db-after tx)
(map :e (:tx-data tx))))
(throw (ex-info "No new invoices found."
{})))
tx))))
(alog/info ::importing-uploaded :count (count imports))
(let [potential-invoices (->> imports
(map import->invoice)
(map #(validate-invoice % user))
admin-only-if-multiple-clients
(mapv d-invoices/code-invoice)
(mapv (fn [i] [:propose-invoice i])))]
(alog/info ::creating-invoice :invoices potential-invoices)
(let [tx (audit-transact potential-invoices user)]
(when-not (seq (dc/q '[:find ?i
:in $ [?i ...]
:where [?i :invoice/invoice-number]]
(:db-after tx)
(map :e (:tx-data tx))))
(throw (ex-info "No new invoices found."
{})))
tx)))
(defn validate-account-rows [rows code->existing-account]
(when-let [bad-types (seq (->> rows
@@ -390,7 +388,8 @@
:body (pr-str stats)
:headers {"Content-Type" "application/edn"}})
(catch Exception e
(log/error e)
(alog/error ::couldnt-batch-upload
:error e)
{:status 500
:body (pr-str {:message (.getMessage e)
:error (.toString e)
@@ -412,8 +411,7 @@
vendor (some-> (or vendor vendor-2)
(Long/parseLong))
{:keys [filename tempfile]} files]
(lc/with-context {:parsing-file filename}
(log/info tempfile)
(mu/with-context {:parsing-file filename}
(try
(let [extension (last (str/split (.getName (io/file filename)) #"\."))
s3-location (str "invoice-files/" (str (UUID/randomUUID)) "." extension)
@@ -437,7 +435,8 @@
:body (pr-str {})
:headers {"Content-Type" "application/edn"}}
(catch Exception e
(log/warn e)
(alog/warn ::couldnt-import-upload
:error e)
{:status 400
:body (pr-str {:message (.getMessage e)
:error (.toString e)
@@ -486,7 +485,7 @@
:body (pr-str {})
:headers {"Content-Type" "application/edn"}}
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 500
:body (pr-str {:message (.getMessage e)
:error (.toString e)
@@ -509,7 +508,7 @@
:body (import-account-overrides client (.getPath tempfile))
:headers {"Content-Type" "application/json"}}
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 500
:body {:message (.getMessage e)
:data (ex-data e)}

View File

@@ -7,14 +7,14 @@
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.logging :as log]
[auto-ap.logging :as alog]
[com.unbounce.dogstatsd.core :as statsd]
[com.brunobonacci.mulog :as mu]
[config.core :refer [env]]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]
[ring.middleware.json :refer [wrap-json-response]]
[ring.util.request :refer [body-string]]
[unilog.context :as lc])
[ring.util.request :refer [body-string]])
(:import
(java.util UUID)))
@@ -29,12 +29,11 @@
(defn execute-query [query-params params]
(let [{:keys [query-id]} params]
(lc/with-context {:query-id query-id}
(mu/with-context {:query-id query-id}
(log/info "Executing raw query " query-id)
(alog/info ::executing-query :query-id query-id)
(let [query-string (str (slurp (:object-content (s3/get-object :bucket-name (:data-bucket env)
:key (str "queries/" (:query-id params))))))]
(log/info "Executing query " query-string)
(into (list) (apply dc/q (edn/read-string query-string)
(into [(dc/db conn)] (edn/read-string (get query-params "args" "[]")))))))))
@@ -69,7 +68,6 @@
(assert-admin identity)
(let [obj (s3/list-objects :bucket-name (:data-bucket env)
:prefix (str "queries/"))]
(log/info obj)
{:body (->> (:object-summaries obj)
(map (fn [o]
{:last-modified (.toString (:last-modified o))
@@ -77,7 +75,6 @@
(defn create-query [{:keys [query-params identity] :as request}]
(assert-admin identity)
(log/info "Note" (query-params "note"))
(put-query (str (UUID/randomUUID)) (body-string request) (query-params "note")))
(defn get-query [{:keys [identity params]} ]
@@ -86,7 +83,6 @@
obj (s3/get-object :bucket-name (:data-bucket env)
:key (str "queries/" query-id))
query-string (str (slurp (:object-content obj)))]
(log/info obj)
{:body {:query query-string
:note (:note (:user-metadata (:object-metadata obj)))
:id query-id
@@ -95,7 +91,6 @@
(defn update-query [{:keys [query-params identity params] :as request} ]
(assert-admin identity)
(log/info "Note" (query-params "note"))
(put-query (:query-id params) (body-string request) (query-params "note")))
(defn results-json-query [{:keys [query-params params]}]
@@ -108,7 +103,6 @@
obj (s3/get-object :bucket-name (:data-bucket env)
:key (str "queries/" query-id))
query-string (str (slurp (:object-content obj)))]
(log/info obj)
{:body query-string}))
(defn results-csv-query [{:keys [query-params params]}]

View File

@@ -5,7 +5,7 @@
[auto-ap.graphql.utils :refer [assert-admin assert-can-see-client]]
[auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.yodlee.core2 :as yodlee]
[clojure.tools.logging :as log]
[auto-ap.logging :as alog]
[config.core :refer [env]]
[datomic.api :as dc]))
@@ -25,7 +25,7 @@
:url (:yodlee2-fastlink env)}) }))
(defn refresh-provider-accounts [{:keys [identity edn-params]}]
(assert-admin identity)
(log/info "refreshing " edn-params)
(alog/info ::refreshing :params edn-params)
(try
(yodlee/refresh-provider-account (-> (:client-id edn-params)
Long/parseLong
@@ -36,7 +36,7 @@
:headers {"Content-Type" "application/edn"}
:body "{}" }
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 400
:headers {"Content-Type" "application/edn"}
:body (pr-str {:message (.getMessage e)
@@ -45,7 +45,9 @@
(defn get-provider-account-detail [{:keys [identity]
{:keys [client id]} :route-params}]
(assert-admin identity)
(log/info "looking-up " client id)
(alog/info ::looking-up
:client client
:id id)
(try
{:status 200
@@ -56,7 +58,7 @@
:client/code)
id))}
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 400
:headers {"Content-Type" "application/edn"}
:body (pr-str {:message (.getMessage e)
@@ -75,7 +77,7 @@
(Long/parseLong id)
(dissoc data :client-id )))}
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 500
:headers {"Content-Type" "application/edn"}
:body (pr-str {:message (.getMessage e)
@@ -93,7 +95,7 @@
:headers {"Content-Type" "application/edn"}
:body (pr-str {}) }
(catch Exception e
(log/error e)
(alog/error ::error :error e)
{:status 400
:headers {"Content-Type" "application/edn"}
:body (pr-str {:message (.getMessage e)