Which node logger did you use in your project?

Following are the common logger modules:

1) logger

SUMMARY

A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()

USAGE

A logger has 5 different levels of logging in a specific order:
'fatal', 'error', 'warn', 'info', 'debug'
Each of these log levels has its own method on the logging instance. You can set the maximum log level on a logger at runtime.
By default, a logger writes to STDOUT, but given a writeable file path, it will log directly to a file.

Instantiation:



// node/common.js style 
var logger = require('./logger').createLogger(); // logs to STDOUT
var logger = require('./logger').createLogger('development.log'); // logs to a file


2) simple-node-logger

A simple multi-level logger for console, file, and rolling file appenders. Features include:
  • levels: trace, debug, info, warn, error and fatal levels (plus all and off)
  • flexible appender/formatters with default to HH:mm:ss.SSS LEVEL message
  • add appenders to send output to console, file, rolling file, etc
  • change log levels on the fly
  • domain and category columns
  • overridable format methods in base appender
  • stats that track counts of all log statements including warn, error, etc
  • ability to configure to emit process error event for central trapping

How to use

// create a stdout console logger
const log = require('simple-node-logger').createSimpleLogger();
or
// create a stdout and file logger
const log = require('simple-node-logger').createSimpleLogger('project.log');
or
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger'),
    opts = {
        logFilePath:'mylogfile.log',
        timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
    },
log = SimpleNodeLogger.createSimpleLogger( opts );
or
// create a file only file logger
const log = require('simple-node-logger').createSimpleFileLogger('project.log');
or
// create a rolling file logger based on date/time that fires process events
const opts = {
    errorEventName:'error',
        logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
        fileNamePattern:'roll-<DATE>.log',
        dateFormat:'YYYY.MM.DD'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
3) log4js

Features
  • coloured console logging to stdout or stderr
  • file appender, with configurable log rolling based on file size or date
  • a logger for connect/express servers
  • configurable log message layout/patterns
  • different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)

Usage

Minimalist version:
var log4js = require('log4js');
var logger = log4js.getLogger();
logger.level = 'debug';
logger.debug("Some debug messages");
4) Winston

Comments

Popular posts from this blog

What test framework did you use to test your nodejs applications

What is V8 Engine? What is the relationship between Node.js and V8?