Skip to content

Instantly share code, notes, and snippets.

@niradler
Created July 23, 2019 19:55
Show Gist options
  • Save niradler/907a0d1a511e9f9735b240142e2383e1 to your computer and use it in GitHub Desktop.
Save niradler/907a0d1a511e9f9735b240142e2383e1 to your computer and use it in GitHub Desktop.
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