Last active
July 12, 2018 11:40
-
-
Save Raidus/2f9622d5d9d04e23ca00ae13954fd665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const winston = require('winston'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const dir = path.resolve(__dirname, '../logs/'); | |
if (!fs.existsSync(dir)) { | |
fs.mkdirSync(dir); | |
} | |
// set default log level. | |
const logLevel = 'info'; | |
// Set up logger | |
const customColors = { | |
trace: 'white', | |
debug: 'green', | |
info: 'blue', | |
warn: 'yellow', | |
crit: 'red', | |
fatal: 'red', | |
}; | |
const logger = filename => | |
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: `./logs/${filename}` }), | |
], | |
}); | |
winston.addColors(customColors); | |
// Extend logger object to properly log 'Error' types | |
const origLog = logger.log; | |
logger.log = function(level, msg) { | |
if (msg instanceof Error) { | |
let args = Array.prototype.slice.call(arguments); | |
args[1] = msg.stack; | |
origLog.apply(logger, args); | |
} else { | |
origLog.apply(logger, arguments); | |
} | |
}; | |
/* | |
npm install [email protected] | |
*/ | |
/* LOGGER EXAMPLES | |
const log = require('../helpers/log.js')('somelogs'); | |
log.trace('testing'); | |
log.debug('testing'); | |
log.info('testing'); | |
log.warn('testing'); | |
log.crit('testing'); | |
log.fatal({ test: 'testing' }); | |
*/ | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment