Chore: again, improve k8s error logging

This commit is contained in:
shamoon
2026-08-21 08:11:48 -07:00
parent cc22d04429
commit fa47a2d86c
2 changed files with 16 additions and 13 deletions
+6 -13
View File
@@ -18,23 +18,16 @@ function combineMessageAndSplat() {
} }
function messageFormatter(logInfo) { function messageFormatter(logInfo) {
if (logInfo.label) { const label = logInfo.label ? `<${logInfo.label}> ` : "";
if (logInfo.stack) { // e.g. fetch errors say nothing useful without the cause
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.stack}`; const cause = logInfo.cause ? `\ncaused by: ${logInfo.cause.stack ?? logInfo.cause}` : "";
} return `[${logInfo.timestamp}] ${logInfo.level}: ${label}${logInfo.stack || logInfo.message}${cause}`;
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.message}`;
}
if (logInfo.stack) {
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
}
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
} }
function getConsoleLogger() { function getConsoleLogger() {
return new winston.transports.Console({ return new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
winston.format.errors({ stack: true }), winston.format.errors({ stack: true, cause: true }),
combineMessageAndSplat(), combineMessageAndSplat(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.colorize(), winston.format.colorize(),
@@ -51,7 +44,7 @@ function getFileLogger() {
return new winston.transports.File({ return new winston.transports.File({
format: winston.format.combine( format: winston.format.combine(
winston.format.errors({ stack: true }), winston.format.errors({ stack: true, cause: true }),
combineMessageAndSplat(), combineMessageAndSplat(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.printf(messageFormatter), winston.format.printf(messageFormatter),
+10
View File
@@ -170,6 +170,16 @@ describe("utils/logger", () => {
}); });
expect(plainMsg).toBe("[t] info: hello"); expect(plainMsg).toBe("[t] info: hello");
const causeMsg = formatter({
timestamp: "t",
level: "error",
label: "x",
stack: "STACK",
message: "fetch failed",
cause: { stack: "CAUSE STACK" },
});
expect(causeMsg).toBe("[t] error: <x> STACK\ncaused by: CAUSE STACK");
const out = splat.transform( const out = splat.transform(
{ {
message: "Hello %s", message: "Hello %s",