GithubHelp home page GithubHelp logo

likestream / ring.middleware.logger Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pjlegato/ring.middleware.logger

1.0 2.0 0.0 121 KB

Ring middleware to log each request using Log4J

Clojure 100.00%

ring.middleware.logger's Introduction

ring.middleware.logger

Ring middleware to log each request using Log4J.

This is ALPHA software; the internals and API can change at any time.

Usage

In your project.clj, add the following dependency:

[ring.middleware.logger "0.2.0"]

Then, just add the middleware to your stack. It comes preconfigured with reasonable defaults, which append ANSI colorized log messages on each request to logs/ring.log.

In Noir, setup is as simple as:

(ns my.web.server
  (:require
     [noir.server]
     [ring.middleware.logger :as logger]))

(defonce logger-middleware (noir.server/add-middleware logger/wrap-with-logger))
;; Then start the server normally

If you'd prefer plaintext logging without the ANSI colors, just use:

(defonce logger-middleware (nr-server/add-middleware logger/wrap-with-plaintext-logger))

What Gets Logged

The default setup logs:

  • an :info-level message when a request begins;
  • an :info level message when a response is generated without any server errors (i.e. its HTTP status is < 500);
  • an :error level message when a response's HTTP status is >= 500;
  • an :error level message with a stack trace when an exception is thrown during response generation.

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.

Logging Other Data

By default, the log backend is set up to log only messages generated from the ring.middleware.logger namespace. You can optionally use the same log facility used by the logger middleware to log any other data you like.

If you want your entire app to log to the ring.middleware.logger backend, call:

 (logger/set-default-root-logger!)

If you only want a particular namespace to be directed to this backend, call

 (logger/set-default-logger! "some.namespace")
 (logger/set-default-logger!) ;; auto-uses the namespace of the calling context

Then, you can use clojure.tools.logging functions normally, e.g.

(clojure.tools.logging/error "Some error message")
(clojure.tools.logging/info "An informational message")

Customization

Every aspect of the logging can be customized, and a few presets are provided.

You can supply your own logger function and your own prefix specification (the part that does the timestamp and log level at the beginning of the line) for further customization. Take a look at logger.clj to see what's available.

License

ring.middleware.logger is by Paul Legato. Copyright (C) 2012 Spring Semantics Inc. Distributed under the Eclipse Public License, the same as Clojure.

ring.middleware.logger's People

Contributors

dcj avatar pjlegato avatar

Watchers

Richard Newman avatar James Cloos avatar

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.