Giter Site home page Giter Site logo

ring-logger's Introduction

Ring-logger Circle CI

Ring middleware to log response time and other details of each request that arrives to your server.

  • Logs request start, finish, parameters and exceptions by default.
  • The user can choose which of those messages to log by using the more specific middleware.
  • "Logs as data": Log messages are simple clojure maps. You can provide a transform-fn to transform to other representations (string, JSON).
  • Uses clojure.tools.logging by default, accepts a log-fn for switching to other log backends (timbre, etc.)

DOCUMENTATION | 0.7.x

Clojars Project

Getting started

Add the dependency to your project.

Leiningen:

[ring-logger "1.1.1"]

deps.edn:

ring-logger/ring-logger {:mvn/version "1.1.1"}

Usage

Add the middleware to your ring stack:

    (ns foo
      (:require [ring.adapter.jetty :as jetty]
                [ring.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})

Example output:

INFO  ring.logger: {:request-method :get, :uri "/", :server-name "localhost", :ring.logger/type :starting}
DEBUG ring.logger: {:request-method :get, :uri "/", :server-name "localhost", :ring.logger/type :params, :params {:name "ring-logger", :password "[REDACTED]"}}
INFO  ring.logger: {:request-method :get, :uri "/", :server-name "localhost", :ring.logger/type :finish, :status 200, :ring.logger/ms 11}

Advanced usage

ring.logger comes with more fine-grained middleware apart from wrap-with-logger:

  • wrap-log-request-start: Logs the start of the request
  • wrap-log-response
  • wrap-log-request-params: Logs the request parameters, using redaction to hide sensitive values (passwords, tokens, etc)

To log just the start and finish of requests (no parameters):

    (-> handler
        logger/wrap-log-response
        ;; more middleware to parse params, cookies, etc.
        logger/wrap-log-request-start)

To measure request latency, wrap-log-response will use the ring.logger/start-ms key added by wrap-log-request-start if both middlewares are being used, or will call System/currentTimeMillis to obtain the value by itself.


To explicitly log start, finish, and parameters of requests:

    (-> (handler)
        wrap-log-response
        ;; the following line must come before `wrap-params`
        (wrap-log-request-params {:transform-fn #(assoc % :level :info)})
        wrap-keyword-params        ;; optional
        wrap-params                ;; required
        wrap-log-request-start)

Note that in the above example, the :transform-fn is optional. You can either bump your log level to DEBUG or use :transform-fn to adjust the log level of params logging before it hits the log-fn. The latter is often preferable, since DEBUG tends to be very noisy.

Also see the example.

Using other logging backends

Other logging backends can be plugged by passing the log-fn option. This is how you could use timbre instead of c.t.logging:

    (require '[timbre.core :as timbre])

    (-> handler
        (logger/wrap-log-response {:log-fn (fn [{:keys [level throwable message]}]
                                             (timbre/log level throwable message))}))

What gets logged

  • an :info level message when a request begins.
  • a :debug level message with the request parameters (redacted).
  • an :info level message when a response is returned without server errors (i.e. its HTTP status is < 500), otherwise an :error level message is logged.
  • an :error level message with a stack trace when an exception is thrown during response generation.

All messages will be usually timestamped by your logging infrastructure.

How to disable exceptions logging

This is especially useful when also using ring.middleware.stacktrace.

(wrap-with-logger app {:log-exceptions? false})

Password: "[REDACTED]"

Sensitive information in params and headers can be redacted before it's sent to the logs.

This is very important: Nobody wants user passwords or authentication tokens to get to the logs and live there forever, in plain text, right?

By default, ring-logger will redact any parameter whose name is in the ring.logger/default-redact-key? set (:password, :token, :secret, etc). You can pass your own set or function to determine which keys to redact as the redact-key? option

(wrap-with-logger app {:redact-key? #{:senha :token})

Logging only certain requests

If you wish to restrict logging to certain paths (or other conditions), combine ring-logger with ring.middleware.conditional, like so:

(:require [ring.middleware.conditional :as c :refer  [if-url-starts-with
                                                      if-url-doesnt-start-with
                                                      if-url-matches
                                                      if-url-doesnt-match]])

(def my-ring-app
   (-> handler
       (if-url-starts-with "/foo" wrap-with-logger)

        ;; Or:
        ;; (c/if some-test-fn wrap-with-logger)
        ;; etc.

       wrap-with-other-handler))

Consult the ring.middleware.conditional docs for full details.

Similar projects

pjlegato/ring.middleware.logger: ring-logger started as a fork of ring.middleware.logger. It's a great option if you don't mind pulling a transitive dependency on onelog & log4j.

lambdaisland/ring.middleware.logger: a fork of r.m.logger that replaces onelog with clojure.tools.logging

Contributing

Pull requests are welcome!

License

Copyright (C) 2015-2018 Nicolás Berger Copyright (C) 2012-2014 Paul Legato.

Distributed under the Eclipse Public License, the same as Clojure.

ring-logger's People

Contributors

bdollard avatar cambodiancoder avatar deobald avatar emlyn avatar fajpunk avatar grinderrz avatar itmeze avatar jaimeagudo avatar kauko avatar miikka avatar mtkp avatar nberger avatar phss avatar pjlegato avatar pkpkpk avatar pmensik avatar ray1729 avatar yanatan16 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ring-logger's Issues

Avoid logging only request-params

I'm implementing a custom printer multimethods to starting

(defmethod starting :my-starting
  [{:keys [logger] :as options} req]
  (info (ansi/style "Starting " :cyan)
        (select-keys req [:character-encoding
                          :content-length
                          :content-type
                          :query-string
                          :remote-addr
                          :request-method
                          :scheme
                          :server-name
                          :server-port
                          :uri
                          :identity])))

;;;
(wrap-with-logger {:printer :my-starting})

To avoid the others logs, I used only by the level of the log (request-details, sending-response), But the request-params it is also info as the startingand finished.
So, How can I stop log this request-params?

wrap-log-request-params doesn't work

It is simply ignored.

I'm running Clojure 1.10 on Linux Mint Cinnamon 19 64-bits.

I've tried all these:

(defn -main
  [& args]
  (-> app
    logger/wrap-with-logger
    logger/wrap-log-request-params
    logger/wrap-log-request-start
    logger/wrap-log-response
    (run-jetty {:port (or (some-> args first Integer/parseInt) 3000)})))
(defn -main
  [& args]
  (-> app
    logger/wrap-log-request-params
    (run-jetty {:port (or (some-> args first Integer/parseInt) 3000)})))
(defn -main
  [& args]
  (-> app
    logger/wrap-with-logger
    logger/wrap-log-request-params
    (run-jetty {:port (or (some-> args first Integer/parseInt) 3000)})))
(defn -main
  [& args]
  (-> app
    logger/wrap-log-request-params
    logger/wrap-log-request-start
    logger/wrap-log-response
    (run-jetty {:port (or (some-> args first Integer/parseInt) 3000)})))

Logging Request Body

Hi, thanks for working on this. Is there a way to log the request body? If so, I haven't been able to figure it out.

I expected to pass :body to the :request-keys option in wrap-with-logger, but this errors with com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class org.eclipse.jetty.server.HttpInputOverHTTP: HttpInputOverHTTP@1a03aea5[c=0,q=0,[0]=null,s=STREAM]

Thanks.

Started using timbre ring-logger and seeing a mysterious nil printed at the start of each log line

Hi Guys,

[02/10/17 08:03:05.194] I - nil Finished :get <URL-obscured> for 127.0.0.1 in (38 ms) Status: 200

I see the nil printed for both Starting and Finished but I don't see it for the exception output.

Does anyone know where I could look to see about how to figure out what this is supposed to be -- some variable is being printed with value nil obviously, but I'm not sure how to find it.

Thanks for your time.

Jason.

wrap-log-response

For tracing and analytics it would be really useful to see request and response as well in one log message. Currently, as far as I understand there is no possibility to log response at all.

Is there any plans on it?

Ring-logger 1.0.0: Getting back to the roots

EDIT: Looking into how different the new version is compared to the 0.7.x series, I decided to bump the new version to 1.0.0. It should help people realize that this is a major release that might (and actually does) contain breaking changes.


I've been feeling some discomfort when thinking about adding new features to ring.logger, or even when trying to use it! What the library provides is quite simple: an easy way to log some information about each request, in a way that is useful for monitoring in a production environment, but also for debugging in development.

I tried to make it customizable/adaptable by using multimethods, but in retrospect it doesn't sound like the best approach: As a user of the library, if I have to find out what multimethods to implement, and basically copy & paste the original fn to adapt it to my needs, then I will probably ditch the library and just add my own middleware. That what I (me, the maintainer of the library) have done on my latest projects. There must be a better way.

Long live ring-logger!

So here's the deal: I'll be working on a new version of the library which will have a more minimalist core, will accept a fn to change the output format (logs are not made of strings only), and will have minimal configuration options (with sensible defaults) for things like hiding sensitive values (redact-keys) or choosing which aspects of the request map to log. Might also leave that part to the output-fn.

The goal is to make it a no-brainer to use it on... let's say 80% of ring-based projects. Or well, at least on 100% of my own projects.


PS: I'd like to thank @pjlegato for creating the original ring.middleware.logger, and to all the current and past contributors, ring-logger wouldn't have gotten up to this point without your help!

Redacted key is not removed from URL

Our application has to send user_token query parameter when doing authentication with websocket (which doesn't support Authorization header). The value is correctly redacted in Params map (based on redact-keys config), however it remains in the URL so the token is readable by anybody with access to the log . Can you please look at it?

Filter query string

I have a query string like this: /login?access_token=xxx123. ring-logger exposes redact-keys to filter access_token from the request params, but it's not filtered from the query-string. Any suggestions for filtering it? I'd be happy to submit a pull request.

wrap-with-body logger missing timbre

Can you give a better idea on how to call wrap-with-body-logger if we are using timbre.
The function definition seems to be different than that of wrap-with-logger which I'm not sure why.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.