Skip to content

Instantly share code, notes, and snippets.

@santhoshtr
Last active October 17, 2018 12:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save santhoshtr/10002263 to your computer and use it in GitHub Desktop.
Save santhoshtr/10002263 to your computer and use it in GitHub Desktop.
Winston based configurable node logger
var winston = require( 'winston' ),
fs = require( 'fs' ),
logDir = 'log', // Or read from a configuration
env = process.env.NODE_ENV || 'development',
logger;
winston.setLevels( winston.config.npm.levels );
winston.addColors( winston.config.npm.colors );
if ( !fs.existsSync( logDir ) ) {
// Create the directory if it does not exist
fs.mkdirSync( logDir );
}
logger = new( winston.Logger )( {
transports: [
new winston.transports.Console( {
level: 'warn', // Only write logs of warn level or higher
colorize: true
} ),
new winston.transports.File( {
level: env === 'development' ? 'debug' : 'info',
filename: logDir + '/logs.log',
maxsize: 1024 * 1024 * 10 // 10MB
} )
],
exceptionHandlers: [
new winston.transports.File( {
filename: 'log/exceptions.log'
} )
]
} );
module.exports = logger;
// Use this singleton instance of logger like:
// logger = require( 'Logger.js' );
// logger.debug( 'your debug statement' );
// logger.warn( 'your warning' );
@FelipeJz
Copy link

FelipeJz commented Aug 5, 2016

Awesome wrapper, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment