Skip to content

Instantly share code, notes, and snippets.

@prantlf
Forked from rtgibbons/logger.js
Last active February 22, 2022 09:56
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save prantlf/8e0bda1570fa91652767 to your computer and use it in GitHub Desktop.
Logger Library with winston - Fixed for additional log arguments
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) {
if (msg instanceof Error) {
var args = Array.prototype.slice.call(arguments);
args[1] = msg.stack;
origLog.apply(logger, args);
} else {
origLog.apply(logger, arguments);
}
};
/* 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;
@thedug
Copy link

thedug commented Mar 7, 2015

How do the "LOGGER EXAMPLES" work?

I don't see app.logger get set anywhere.

@gabssnake
Copy link

Why require lodash if it is not being used ?

@vikas5914
Copy link

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