Skip to content

Instantly share code, notes, and snippets.

@romant
Forked from santhoshtr/Logger.js
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romant/5e372e2e1ce2bbebc285 to your computer and use it in GitHub Desktop.
Save romant/5e372e2e1ce2bbebc285 to your computer and use it in GitHub Desktop.
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