logger-switch
A simple library to turn ON/OFF logging activity, or enable only a few loggers or even enable/disable logging for a particular code etc."
Last updated 3 years ago by akashbabu .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install logger-switch 
SYNC missed versions from official npm registry.

logger-switch

Nodejs library to turn ON/OFF logging and also deactivate/activate logging only in a particular peice of code, or use your own logger etc.

Installation

npm install logger-switch --save

Usage

var Logger = require('logger-switch');

var logger = new Logger('Test');

logger.activate();
logger.timestamp('DD MMM YY, HH:mm a');

logger.log('hi' + ' Cavin', "how r u??");
//Test (12 Feb 17, 11:37 am): hi Cavin how r u??  

logger.deactivate();
logger.error('This will not be logger');
// This will not be printed

logger.activate();
logger.timestamp(null);
logger.log('this will not have timstamp')
//Prints: Test: this will not have timstamp

Parser Usage

// file contents(./file.log):
// GET /data
// POST /login
// GET /timestamp

var Parser = require("logger-switch/parser")
var parser = new Parser({method: 0})
var file = "./test.log"
var writer = fs.createWriteStream(file)
writer.write("GET /timestamp\n")
writer.write("GET /data\n")
writer.write("POST /login\n")
writer.end()

function validation (param) {
    return (param == "GET")
}

parser.parse("method", validation, file, function(err, out) {
    var res = []
    out.on('data', function(d) {
        res.push(d)
    })
    out.on('end', function() {
        res.length.should.be.eql(2)
        res[0].should.contain("GET /timestamp")
        res[1].should.contain("GET /data")

        fs.unlink(file)
        done()
    })
})

Constructor

Logger(name, logger)
name - Prefix to be used while logging. default is 'Log :'
logger - Custom logger to be used. default is stdout and stderr

Parser(options) options - Should be in the format key-index, where key is for developer reference and index is the index of the key in each line when seperated by " "(space)

Methods - Logger

activate()
Will Enable logging activity

deactivate() Will disable logging activity

timestamp(format) Will enable/disable timestamp logging.
format - Supports momentjs timestamp format string. if null is passed then timestamp logging will be disabled

Methods - Parser

parse([key], [validation], file, cb)
Will parse the given file
key - the key in the options to be used to find the parameter. Defaults to timestamp with index 0
validation - Validation function to be called for the param. This is used to filter the lines in the result. Defaults to function(){return true}
file - Complete file path that has to be parsed
cb - Callback(err, result), result includes the filtered lines in the file

Testing

npm install
npm test

Similarly you can create many such logger and activate only the required logger from the configuration file, depending on env being used.
For more examples

Current Tags

  • 3.0.1                                ...           latest (3 years ago)

7 Versions

  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (5)

Copyright 2014 - 2016 © taobao.org |