GithubHelp home page GithubHelp logo

arnauaregall / node-mdc-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ndemengel/node-mdc-example

0.0 2.0 0.0 136 KB

Example implementation of a java-like Mapped Diagnostic Context (MDC) for logging contextual information in a Web application

JavaScript 100.00%

node-mdc-example's Introduction

node-mdc-example

Example implementation of a java-like Mapped Diagnostic Context (MDC) for logging contextual information in a Web application.

Common Java loggers (Log4j, Logback, etc.) implement a Mapped Diagnostic Context (MDC), i.e. a simple map that is associated to the current thread. This allows for storing contextual information to be logged together with whatever message is emitted by the application.
Typically one will want to log:

  • which user/session is responsible for the currently logged statement (for audit purposes)
  • which request led to a given action (for debugging purposes, how actions are correlated one to each-other)

Being fully asynchronous, nodejs does not allow for attaching such a context to a thread. Fortunately, some smart people are currently [introducing into node] (http://nodejs.org/docs/v0.11.11/api/process.html#process_async_listeners) a way of following asynchronous execution stacks, to which a context map can be attached.
For now, a shim is available, and node-continuation-local-storage already allows for persisting contexts.

This example uses continuation-local-storage to initiate a logging context for each request, that will then be accessed every time something is logged. To demonstrate that the logging context is correctly persisted, the logging utility is used within asynchronous actions.

To be somehow representative of a real production app, this example uses Express and Bunyan.

Points of interest are:

You can run the demo as follow:

  • Run npm install to load the dependencies.
  • Run ./bin/www to start the application.
  • To display nice logs instead of raw JSON ones, you may want to pipe the output of the previous command into the bunyan utility:
    ./bin/www | ./node_modules/.bin/bunyan
  • Open your favorite browser and navigate to http://localhost:3000.
  • Click on the various "Action" links, and clear the session cookie at will with the dedicated button.
  • Observe the logs:
    • the requestId and sessionId fields are persisted across asynchronous calls ;
    • the requestId field is unique to each request ;
    • the sessionId field is unique to each session.

Additionally, you can observe that concurrent uses of the asynchronous functions do not mix logging contexts by running the following command:

./bin/concurrency-test | ./node_modules/.bin/bunyan

node-mdc-example's People

Contributors

ndemengel avatar

Watchers

 avatar  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.