-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cookbook entry on overriding logging
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns logging.service | ||
(:require [io.pedestal.http :as http] | ||
[io.pedestal.http.route :as route] | ||
[io.pedestal.interceptor :refer [interceptor]] | ||
[io.pedestal.log :as log] | ||
[ring.util.response :as ring-resp])) | ||
|
||
(def log-request | ||
"Logs all http requests with response time." | ||
{:name ::log-request | ||
:enter (fn [context] | ||
(assoc-in context [:request :start-time] (System/currentTimeMillis))) | ||
:leave (fn [context] | ||
(let [{:keys [uri start-time request-method]} (:request context) | ||
finish (System/currentTimeMillis) | ||
total (- finish start-time)] | ||
(log/info :msg "request completed" | ||
:method (clojure.string/upper-case (name request-method)) | ||
:uri uri | ||
:status (:status (:response context)) | ||
:response-time total)))}) | ||
|
||
(defn home-page | ||
[request] | ||
(ring-resp/response "Hello World!")) | ||
|
||
(def routes #{["/" :get `home-page]}) | ||
|
||
(def service {::http/routes routes | ||
::http/request-logger (interceptor log-request) | ||
::http/type :jetty | ||
::http/port 8080}) |