Created
July 23, 2019 19:55
-
-
Save niradler/907a0d1a511e9f9735b240142e2383e1 to your computer and use it in GitHub Desktop.
This file contains 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 logform = require('logform'); | |
const tripleBeam = require('triple-beam'); | |
const winston = require('winston'); | |
const errorHunter = logform.format(info => { | |
if (info.error) return info; | |
const splat = info[tripleBeam.SPLAT] || []; | |
info.error = splat.find(obj => obj instanceof Error); | |
return info; | |
}); | |
const errorPrinter = logform.format(info => { | |
if (!info.error) return info; | |
// Handle case where Error has no stack. | |
const errorMsg = info.error.stack || info.error.toString(); | |
info.message += `\n${errorMsg}`; | |
return info; | |
}); | |
const winstonConsoleFormat = logform.format.combine( | |
errorHunter(), | |
errorPrinter(), | |
logform.format.printf(info => `${info.level}: ${info.message}`) | |
); | |
// Configure Winston | |
const logger = winston.createLogger({ | |
transports: [new winston.transports.Console({ | |
format: winstonConsoleFormat, | |
})], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment