Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Logger Library with winston
const { createLogger, format, transports } = require('winston')
const moment = require('moment')
// set default log level.
const logLevel = 'info'
var logger = createLogger({
level: logLevel,
levels: {
fatal: 0,
crit: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5
},
format: format.combine(
format.prettyPrint(),
format.timestamp({
format: 'DD-MM-YYYY hh:mm:ss A'
}),
format.printf(nfo => {
return `${nfo.timestamp} - ${nfo.level}: ${nfo.message}`
})
),
transports: [
new transports.Console(),
new transports.File({
filename: 'bot.log'
})
]
})
// 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
var log = require('./log.js')
log.trace('testing')
log.debug('testing')
log.info('testing')
log.warn('testing')
log.crit('testing')
log.fatal('testing')
*/
module.exports = logger
=
@vikas5914
Copy link
Author

vikas5914 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'

@sethukumar0001
Copy link

sethukumar0001 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

@aabrapp
Copy link

aabrapp commented Apr 27, 2020

FYI, this does not work with 3.x version of logger. check this for 3.x version - https://gist.github.com/aabrapp/984834cf319a2c6e965cacc52caccde8

@vikas5914
Copy link
Author

vikas5914 commented Apr 27, 2020

@aabrapp I have updated for 3.x Version.

@TitusRobyK
Copy link

TitusRobyK commented May 20, 2020

somehow trace is not working

@TitusRobyK
Copy link

TitusRobyK commented May 20, 2020

also it doesnot write the logs into the bot.log

@TitusRobyK
Copy link

TitusRobyK commented May 20, 2020

I have corrected the code , write logs into the respective file

const fs = require('fs');
const {
  createLogger,
  transports,
  format
} = require('winston');

const env = process.env.NODE_ENV;
const logDir = 'logs';

if (!fs.existsSync(logDir)) {
  fs.mkdirSync(logDir);
}

const now = new Date();
const logLevel = 'info'

var logger = createLogger({
  level: logLevel,
  levels: {
    fatal: 0,
    crit: 1,
    warn: 2,
    info: 3,
    debug: 4,
    trace: 5
  },
  format: format.combine(
    format.prettyPrint(),
    format.timestamp({
      format: 'DD-MM-YYYY hh:mm:ss A'
    }),
    format.printf(nfo => {
      return `${nfo.timestamp} - ${nfo.level}: ${nfo.message}`
    })
  ),
  transports: [
    new transports.Console(),
    new transports.File({
      filename: `${logDir}/app.log`,
    })
  ]
})

// 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)
  }
}

module.exports = logger;

module.exports.stream = {
  write: function (message) {
    logger.info(message);
    console.log('message = ', message);
  },
};

@atjshop
Copy link

atjshop commented Sep 15, 2020

hi, look for the same and this is useful.
questions:

  1. what is the module.exports.stream for? how to use it?
  2. is it possible to capture the caller file, caller function or line number?

thanks!

@newtonjose
Copy link

newtonjose commented May 4, 2021

Working perfect for me. Tks

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