Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Custom Logger Library with winston
var winston = require('winston')
// set default log level.
var logLevel = 'info'
// Set up logger
var customColors = {
trace: 'white',
debug: 'green',
info: 'blue',
warn: 'yellow',
crit: 'red',
fatal: 'red'
var logger = new (winston.Logger)({
colors: customColors,
level: logLevel,
levels: {
fatal: 0,
crit: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5
transports: [
new (winston.transports.Console)({
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) {
if (msg instanceof Error) {
var args =
args[1] = msg.stack
origLog.apply(logger, args)
} else {
origLog.apply(logger, arguments)
var log = require('./log.js')
module.exports = logger

This comment has been minimized.

Copy link
Owner Author

commented Jan 31, 2017

var logLevel = 'info' // Winston will log info and lower than info log level.

So log.trace('testing') and log.debug('testing') will not be log by winston.

If you want to log trace and debug then set var logLevel = 'trace'


This comment has been minimized.

Copy link

commented Sep 7, 2019

how to connect this logger.js in my default app.js in express js and i want console entire log data in one text file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.