Created
November 27, 2013 11:29
-
-
Save lokori/7674235 to your computer and use it in GitHub Desktop.
Example of logging a Ring request with Ring.
(I'm aware there are some existing alternatives for doing this.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns aitu.infra.print-wrapper | |
(:require [clojure.tools.logging :as log])) | |
(def ^:private requestid (atom 1)) | |
(defn http-method->str [keyword-or-str] | |
(clojure.string/upper-case (name keyword-or-str))) | |
(defn log-request-wrapper [ring-handler] | |
"Logging wrapper. Basic info + duration and additional unique id per request to enable performance analysis" | |
(fn [req] | |
(let [id (swap! requestid inc) | |
start (System/currentTimeMillis)] | |
(log/info (str "Request " id " start. " | |
" remote-addr: " (:remote-addr req) | |
" ,method: " (http-method->str (:request-method req)) | |
" ,uri: " (:uri req) | |
" ,query-string: " (:query-string req) | |
" ,user-agent: " (get (:headers req) "user-agent") | |
" ,referer: " (get (:headers req) "referer"))) | |
(let [response (ring-handler req) | |
finish (System/currentTimeMillis) | |
total (- finish start)] | |
(log/info (str "Request " id " end. Duration: " total " ms. uri: " (:uri req))) | |
response | |
)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment