Skip to content

Instantly share code, notes, and snippets.

@ikhsanalatsary
Forked from vikas5914/log.js
Created January 27, 2021 01:32
Show Gist options
  • Save ikhsanalatsary/8265a5f79a637baeb991031c71ef0979 to your computer and use it in GitHub Desktop.
Save ikhsanalatsary/8265a5f79a637baeb991031c71ef0979 to your computer and use it in GitHub Desktop.
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
=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment