Coder Social home page Coder Social logo

primus-logger's Introduction

Primus Logger

Build Status NPM version

primus-logger is a tiny module for the Primus real-time framework that allows you to add context to logging messages.

Even if you're simply outputting data to the console, it is often useful to correlate certain messages with application context (for instance, to log which user was responsible for triggering a certain event). Such metadata usually resides on the Spark object, which this module can dynamically export by prefixing each message with certain object properties.

The modules wraps the winston logging library, which theoretically means that every option supported by winston is also supported by primus-logger (e.g. transport configuration).

Installation

Installation occurs via npm:

npm install primus-logger --save

Usage

Like any other Primus plugin, you only need to create a server instance and use the plugin. It exposes a new option, logging, where all the configuration necessary for the plugin can be defined.

By default, the Console transport of winston is used, which does not use colors or timestamps, with an info level output cut out. The following overrides theses defaults to adds colors and timestamps.

The prefix property uses an object path to get the data from the Spark instance. You can read more about how to formulate your prefix by visiting the object-path project page.

'use strict';

var Primus = require('primus'),
  winston = require('winston');

var server = http.createServer()
  , primus = new Primus(server, {
      logging: {
            prefix: ['id', 'address.ip'],
            transports: [
              new (winston.transports.Console)({
                'timestamp': true,
                'colorize': true
              })
            ]
          }
  });

primus.use('logger', 'primus-logger');

primus.on('connection', function (spark) {
  spark.info('Connected');

  if ('127.0.0.1' !== spark.address.ip) {
    spark.log('error', 'Unexpected ip "' + spark.address.ip + '"');
  }
});

The output on the console will look like this:

2014-01-01T15:00:28.012Z - info: [1388618721010$0/127.0.0.1] Connected
2014-01-01T15:00:28.012Z - error: [1388618721010$0/127.0.0.1] Unexpected ip "127.0.0.2"

Supported levels

The following logging levels are supported on a Spark instance:

  • log (generic, requires a level argument)
  • debug
  • info
  • warn
  • error
  • err (alias of error)

Options

Currently, the only configurable option on the primus-logger plugin, besides dynamic properties (configured as prefix), is the separator string (defaults to /).

Compatibility

This plugin has been tested against Primus 1.5.x only.

License

MIT

primus-logger's People

Stargazers

 avatar  avatar

Watchers

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