-
-
Save romant/5e372e2e1ce2bbebc285 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var winston = require('winston'), | |
fs = require('fs'), | |
logDir = 'log', // Or read from a configuration | |
env = process.env.NODE_ENV || 'development', | |
logger; | |
var Logentries = require('winston-logentries'); | |
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: 'debug', | |
colorize: true, | |
prettyPrint: true | |
}), | |
new winston.transports.Logentries( | |
{ | |
level: 'debug', | |
handleExceptions: true, | |
colorize: true, | |
token: process.env.LOGENTRIES_TOKEN | |
}), | |
new winston.transports.File( | |
{ | |
level: env === 'development' ? 'debug' : 'info', | |
filename: logDir + '/logs.json', | |
json: true, | |
handleExceptions: true, | |
maxsize: 1024 * 1024 * 10 // 10MB, | |
}) | |
], | |
exceptionHandlers: [ | |
new winston.transports.File( | |
{ | |
json: true, | |
filename: 'log/exceptions.json' | |
}) | |
] | |
}); | |
process.on('uncaughtException', function (err) | |
{ | |
logger.error('uncaughtException', | |
{ | |
message: err.message, | |
stack: err.stack | |
}); | |
process.exit(1); | |
}); | |
module.exports = logger; | |
// Use this singleton instance of logger like: | |
// logger = require( 'logHandler.js' ); | |
// logger.debug( 'your debug statement' ); | |
// logger.warn( 'your warning' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment