dlogger

Simple NodeJS logging with color and support for express stream

npm install dlogger
8 downloads in the last month

dLogger

dLogger is a node.js module for simple logging with color and categories option.

Requirements

Install

$ npm install dlogger

Usage

    var info = require('dlogger').use('info', 'green');
    info('hello world');

    var error = require('dlogger').use('error', 'red');
    error('error occured');

    var dbLogger = require('dlogger').use('database', 'yellow');
    dbLogger('saving to [green]database[/green]');

API

require('dlogger').setStream(stream)

Setting the stream to some function ( by default it is console.log ).

  • stream must be a function
    var log = [];
    var stream = function() {
      log.push(Array.prototype.slice.call(arguments));
    }

    var info = require('dlogger').setStream(stream).use('info', 'green');
    info('this will log in the array');

You can set only one stream for your app.

var loggerInstance = require('dlogger').use(name, style)

This creates new logger instance with the specified name and style.

  • name is a string for this instance of the logger ( e.g. fatal, app, database, etc. )
  • style is a string with color and/or text style

  • supported colors are : 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'

  • supported styles are : 'normal', 'bold', 'underline', 'blink'

Returns an instance

loggerInstance(text, [arguments...])

This logs any argument to console.log ( or any other function specified as stream ).

  • text is string which has support for specifying colors and styles in brackets like the example below.
  • arguments are objects to be logged and sent to the stream.
    var info = require('dlogger').use('info', 'green');

    var object = { a: 1, b: 2, c: 3 }

    info('This [green]is[/green] [yellow]info[/yellow] text with attached object to be logged', object);

loggerInstance(text, [arguments...]).and(callback)

  • callback must be a function

Executes the callback immediately after logging. Useful for saving code.

    var fatal = require('dlogger').use('fatal', 'red');

    if ( error ) fatal('Fatal error occured. Exiting ...').and( function() { process.exit(1) } );

Better with CoffeeScript

    fatal = require('dlogger').use 'fatal', 'red'

    if error then fatal('Fatal error occured. Exiting ...').and -> process.exit 1

Copyright (c) 2013 Ivan Drinchev drinchev@gmail.com . See LICENSE.txt for details.

npm loves you