Skip to content

Commit

Permalink
Add cookbook entry on overriding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarroway committed Jan 19, 2021
1 parent 108ed78 commit 07ac6a6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions content/cookbook/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,45 @@ Reference: link:../reference/table-syntax#_http_verb[Table Syntax Http Verb]
----
include::src/verb_neutral_routes/service.clj[tags=routes]
----

== How to override request logging

The `log-request` is always added as the first of the link:../reference/default-interceptors[default interceptors].
This will log requests as they come in (using `io.pedestal.log`), according to how your logging infrastructure
is configured.

The default logger will output something like:

----
INFO io.pedestal.log {:msg "GET /status", :line 80}
----

To disable the log entirely, set the logger to `nil` in the link:../reference/service-map[service map]:

[source,clojure]
----
(-> {::http/request-logger nil}
http/default-interceptors)
----

=== Custom log format

To modify the log message, for example, to log request timing, you can use a custom interceptor.
In this example, we use `io.pedestal.log`, but you can freely use
link:https://github.com/clojure/tools.logging[`org.clojure/tools.logging`] or
any other logging mechanism.

[[cookbook-listing]]
[source,clojure]
----
include::src/logging/service.clj[]
----

This will output something like:

----
INFO logging.service {:method "GET", :msg "request completed", :line "24", :status 200, :uri "/status", :response-time 5}
----

This can obviously be extended to log unique request ids, user details,
`:json-params` from the body or anything else as required.
32 changes: 32 additions & 0 deletions content/cookbook/src/logging/service.clj
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})

0 comments on commit 07ac6a6

Please sign in to comment.