Skip to content

Instantly share code, notes, and snippets.

@rtgibbons
Created November 7, 2013 13:51
Show Gist options
  • Select an option

  • Save rtgibbons/7354879 to your computer and use it in GitHub Desktop.

Select an option

Save rtgibbons/7354879 to your computer and use it in GitHub Desktop.
Logger Library with winston
var app = require(process.cwd() + '/app');
var winston = require('winston');
var _ = require('lodash');
// Set up logger
var customColors = {
trace: 'white',
debug: 'green',
info: 'green',
warn: 'yellow',
crit: 'red',
fatal: 'red'
};
var logger = new(winston.Logger)({
colors: customColors,
levels: {
trace: 0,
debug: 1,
info: 2,
warn: 3,
crit: 4,
fatal: 5
},
transports: [
new(winston.transports.Console)({
level: app.settings.logLevel,
colorize: true,
timestamp: true
})
// new (winston.transports.File)({ filename: 'somefile.log' })
]
});
winston.addColors(customColors);
// Extend logger object to properly log 'Error' types
var origLog = logger.log;
logger.log = function (level, msg) {
var objType = Object.prototype.toString.call(msg);
if (objType === '[object Error]') {
origLog.call(logger, level, msg.toString());
} else {
origLog.call(logger, level, msg);
}
};
/* LOGGER EXAMPLES
app.logger.trace('testing');
app.logger.debug('testing');
app.logger.info('testing');
app.logger.warn('testing');
app.logger.crit('testing');
app.logger.fatal('testing');
*/
module.exports = logger;
@bryanlarsen
Copy link
Copy Markdown

origLog.call(logger, level, msg); will lose additional arguments to the log function. origLog.apply(logger, arguments) works better. I also suggest using msg.stack instead of msg.toString(), but that's a preference.

@prantlf
Copy link
Copy Markdown

prantlf commented Dec 23, 2014

Excellent idea. Fixed at my gist fork.

@coryellenberger
Copy link
Copy Markdown

I see you bring in lodash but never use it; I'm assuming it was brought in for some other reason then forgot?

@dantheman213
Copy link
Copy Markdown

How does this wrap around the expressjs application? what file are you calling this from and how do you redirect the standard output to this library?

@khalilovcmd
Copy link
Copy Markdown

If you would like to add a file transport to a sub-directory, you could do the following:

var fs = require('fs');

// check if directory exist
if (!fs.existsSync('logs')) {
    fs.mkdirSync('logs'); // create new directory
}

And also you don't need require('lodash')

I forked and updated it.

@chaituckr
Copy link
Copy Markdown

for me it is throwing an error saying "cannot find 'logLevel' of undefined" i.e., "app.settings" is undefined.

@joshelson
Copy link
Copy Markdown

If this is not displaying all log levels as expected, note that Winston 2.x reversed the order of the log levels.

Fixed gist here.

@vikas5914
Copy link
Copy Markdown

@BananaAcid
Copy link
Copy Markdown

How to move the monkey-patch of the log function to a proper transform property, this might come in handy: winstonjs/winston#1427 (comment)

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