Skip to content

rapidbizapps/rapid-node-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Rapid Node Logger

A multi-transport async logging library for node.js written on top of winston.

Installation

    git clone https://github.com/rapidbizapps/rapid-node-logger logger

Copy the above folder in node_modules (Remember the folder name should be 'logger')

Usage

Logging

You have to initialise the logger with some properties

app.use(require('logger').init({ requsetKey: "owner" }));
// requestKey is the key name in request object which has user details [req.owner]

Using the Default Logger

The default logger is accessible through the winston module directly. Any method that you could call on an instance of a logger is available on the default logger:

    var Logger = require('logger');
    var logger = new Logger.Logger();

    logger.log('info', 'Hello distributed log files!');
    logger.info('Hello again distributed logs');

    logger.level = 'debug';
    logger.log('debug', 'Now my debug messages are written to console!');

    // Attaching req object as last attribute will log even the 'requsetKey' attr from request object to the log record
    logger.log('debug', 'Now my debug messages are written to console!',req);
    logger.log('debug', {someLog : 'test'},req);
    logger.log({someLog : 'test'},req);
    

By default, only the Console transport and File transport is set on the default logger with the default options. You can add or remove attributes for both this transport by giving in constructor:

    var Logger = require('logger');
    
    var options = {
        console : {
            //console transport options
            silent : true // this is disable logs from prinitng in console 
        },
        file : {
            //file transport options
            
        }
    }
    var logger = new Logger.Logger(options);

For more documentation about working with each individual transport supported by Winston see the

Logging Request and Response

//First Specify the requestKey
app.use(require('logger').init({ requsetKey: "owner" }));

//This middleware will log request and response
app.use(require('logger').logApis);

String interpolation

The log method provides the same string interpolation methods like [util.format][10].

This allows for the following log messages.

logger.log('info', 'test message %s', 'my string');
// info: test message my string

logger.log('info', 'test message %d', 123);
// info: test message 123

logger.log('info', 'test message %j', {number: 123}, {});
// info: test message {"number":123}
// meta = {}

logger.log('info', 'test message %s, %s', 'first', 'second', {number: 123});
// info: test message first, second
// meta = {number: 123}

logger.log('info', 'test message', 'first', 'second', {number: 123});
// info: test message first second
// meta = {number: 123}

Querying Logs

    /*
        logFile : log file name (required)
        rows : number of rows to fetch, Number
        order : desc/asc ,
        from : Date
        until : to date,
        query : query params with regex
        filePath : stores a json file in the given path
        fields : Gives only selected fields
    */
 	var options = {
		logFile: '20170721.log',
		 query: {
		 	"url": "/tas*",
		 	"user._id": [undefined, "de1fc70a037bd8601eb2a0a69492abfe"]
		 },
		//filePath: path.join(__dirname + './../../' + '/logs/sb.json')
	};
  
  logger.query(options, function (err, results) {
    if (err) {
      throw err;
    }
    console.log(results);
  });

Streaming Logs

Streaming allows you to stream your logs back from your chosen transport.

  //
  // Start at the end.
  //
  logger.stream({ start: -1 }).on('log', function(log) {
    console.log(log);
  });

Installation

    git clone https://github.com/rapidbizapps/rapid-node-logger logger

Copy the above folder in node_modules (Remember the folder name should be 'logger')

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published