Ring middleware to log the duration and other details of each request.
The logging backend is pluggable, and defaults to clojure.tools.logging if none is given.
This is ALPHA software; the internals and API can change at any time. Pull requests are welcome!
In your project.clj
, add the following dependency:
[ring.middleware.logger "0.5.0-SNAPSHOT"]
Then, just add the middleware to your stack. It comes preconfigured with reasonable defaults, which append ANSI colorized log messages on each request to whatever logger is in use by clojure.tools.logging.
(ns foo
(:require [ring.adapter.jetty :as jetty]
[ring.middleware.logger :as logger]))
(defn my-ring-app [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello world!"})
(jetty/run-jetty (logger/wrap-with-logger my-ring-app) {:port 8080})
If you'd prefer plaintext logging without the ANSI colors, use
wrap-with-plaintext-logger
instead.
You can supply a map of custom logger functions to wrap-with-logger
,
which will be used instead of the default clojure.tools.logging
functions. The default map is:
{:info (fn [x] (clojure.tools.logging/info x))
:debug (fn [x] (clojure.tools.logging/debug x))
:error (fn [x] (clojure.tools.logging/error x))
:warn (fn [x] (clojure.tools.logging/warn x))}
Replace these functions with whatever logging facility you'd like to use. Each function should take a string and log it at that log level.
The default setup logs:
All messages are timestamped. Each request is assigned a random 4-hex-digit ID, so that different log messages pertaining to the same request can be cross-referenced. These IDs are printed in random ANSI colors by default, for easy visual correlation of log messages while reading a log file.
Copyright (C) 2012-2014 Paul Legato. Distributed under the Eclipse Public License, the same as Clojure.
Can you improve this documentation? These fine people already did:
Paul Legato & sluukkonenEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close