-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_logger.clj
29 lines (26 loc) · 1.15 KB
/
json_logger.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns ring.middleware.json-logger
(:require [clojure.data.json :as json]))
(defn wrap-json-logger [handler]
(fn [request]
(let [start (System/currentTimeMillis)
save-println (fn [line] ; see http://yellerapp.com/posts/2014-12-11-14-race-condition-in-clojure-println.html
(.write *out* (str line "\n"))
(flush))
log (fn [request response]
(-> {:method (:request-method request)
:uri (:uri request)
:query-string (:query-string request)
:remote-addr (:remote-addr request)
:duration (- (System/currentTimeMillis) start)}
(merge response)
(json/write-str :escape-slash false :escape-js-separators false)
save-println))]
(try
(let [response (handler request)]
(log request {:status (:status response)})
response)
(catch Exception e
(log request {:status 500
:exception (str (class e))
:exception-message (.getMessage e)})
(throw e))))))