Last active
May 20, 2021 14:26
-
-
Save jasperkuperus/9df894041e3d5216ce25af03d38ec3f1 to your computer and use it in GitHub Desktop.
Winston / Stackdriver severity level
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 winston = require('winston'); | |
const { LEVEL } = require('triple-beam'); | |
const SeverityLookup = { | |
'default': 'DEFAULT', | |
'silly': 'DEFAULT', | |
'verbose': 'DEBUG', | |
'debug': 'DEBUG', | |
'http': 'notice', | |
'info': 'info', | |
'warn': 'WARNING', | |
'error': 'ERROR', | |
} | |
const stackdriverSeverityFormat = winston.format((info) => ({ | |
...info, | |
// Add severity to your log | |
severity: SeverityLookup[info[LEVEL]] || SeverityLookup['default'], | |
})); | |
const formatters = [ | |
winston.format.timestamp(), | |
// Add the format that supplements the JSON with severity | |
stackdriverSeverityFormat(), | |
winston.format.json(), | |
]; | |
const logger = winston.createLogger({ | |
level: 'silly', | |
format: winston.format.combine(...formatters), | |
stderrLevels: ['error'], | |
transports: [ | |
new winston.transports.Console(), | |
], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @jasperkuperus this has been very helpful !
just in case someone wants to go a step further and preserve the errors in the logged output, here's how we went about it :
It's probably not bulletproof, but it can help too.
We get decent output in GCP with severity and errors, and that is what we wanted from the nodejs-logging-winston lib. It probably offers more features for error handling on the GCP side, but we don't miss it for now
At least now the logs are found when clicking on "instance logs" of a deployment in GCP, which is a breeze :)