Instantly share code, notes, and snippets.

What would you like to do?
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: [
level: app.settings.logLevel,
colorize: true,
timestamp: true
// new (winston.transports.File)({ filename: 'somefile.log' })
// Extend logger object to properly log 'Error' types
var origLog = logger.log;
logger.log = function (level, msg) {
var objType =;
if (objType === '[object Error]') {, level, msg.toString());
} else {, level, msg);
module.exports = logger;

This comment has been minimized.

bryanlarsen commented Feb 19, 2014, 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.


This comment has been minimized.

prantlf commented Dec 23, 2014

Excellent idea. Fixed at my gist fork.


This comment has been minimized.

coryellenberger commented Jul 24, 2015

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


This comment has been minimized.

dantheman213 commented Oct 28, 2015

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?


This comment has been minimized.

khalilovcmd commented Jan 8, 2016

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.


This comment has been minimized.

chaituckr commented May 10, 2016

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


This comment has been minimized.

joshelson commented May 24, 2016

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.


This comment has been minimized.

vikas5914 commented Jan 31, 2017

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