Skip to content

Commit

Permalink
fix: correctly append LodestarError metadata in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Nov 5, 2023
1 parent bf5d92b commit 71ffb8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/logger/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import winston from "winston";
import {isEmptyObject} from "@lodestar/utils";
import {LodestarError, isEmptyObject} from "@lodestar/utils";
import {LoggerOptions, TimestampFormatCode} from "../interface.js";
import {logCtxToJson, logCtxToString, LogData} from "./json.js";
import {formatEpochSlotTime} from "./timeFormat.js";
Expand Down Expand Up @@ -88,7 +88,13 @@ function humanReadableTemplateFn(_info: {[key: string]: any; level: string; mess
str += `[${infoString}] ${info.level.padStart(infoPad)}: ${info.message}`;

if (info.context !== undefined && !isEmptyObject(info.context)) str += " " + logCtxToString(info.context);
if (info.error !== undefined) str += " - " + logCtxToString(info.error);
if (info.error !== undefined)
str +=
// LodestarError is formatted in the same way as context, it is either appended to
// the log message (" ") or extends existing context properties (", "). For any other
// error, the message is printed out and clearly separated from the log message (" - ").
(info.error instanceof LodestarError ? (isEmptyObject(info.context) ? " " : ", ") : " - ") +
logCtxToString(info.error);

return str;
}
21 changes: 19 additions & 2 deletions packages/logger/test/fixtures/loggerFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,27 @@ export const formatsTestCases: (TestCase | (() => TestCase))[] = [
id: "error with metadata",
opts: {module: "test"},
message: "foo bar",
context: {},
error: error,
output: {
human: `[test] \u001b[33mwarn\u001b[39m: foo bar code=SAMPLE_ERROR, data=foo=bar\n${error.stack}`,
json: '{"context":{},"error":{"code":"SAMPLE_ERROR","data":{"foo":"bar"},"stack":"$STACK"},"level":"warn","message":"foo bar","module":"test"}',
},
};
},

() => {
const error = new LodestarError({code: "SAMPLE_ERROR", data: {foo: "bar"}});
error.stack = "$STACK";
return {
id: "error and log with metadata",
opts: {module: "test"},
message: "foo bar",
context: {meta: "data"},
error: error,
output: {
human: `[test] \u001b[33mwarn\u001b[39m: foo bar - code=SAMPLE_ERROR, data=foo=bar\n${error.stack}`,
json: '{"error":{"code":"SAMPLE_ERROR","data":{"foo":"bar"},"stack":"$STACK"},"level":"warn","message":"foo bar","module":"test"}',
human: `[test] \u001b[33mwarn\u001b[39m: foo bar meta=data, code=SAMPLE_ERROR, data=foo=bar\n$STACK`,
json: '{"context":{"meta":"data"},"error":{"code":"SAMPLE_ERROR","data":{"foo":"bar"},"stack":"$STACK"},"level":"warn","message":"foo bar","module":"test"}',
},
};
},
Expand Down

0 comments on commit 71ffb8f

Please sign in to comment.