Skip to content

Commit

Permalink
fix: remove extra white space in logs if context is empty (#6046)
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored Oct 18, 2023
1 parent 3a6702e commit 4fa6327
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/logger/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import winston from "winston";
import {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 @@ -86,7 +87,7 @@ function humanReadableTemplateFn(_info: {[key: string]: any; level: string; mess

str += `[${infoString}] ${info.level.padStart(infoPad)}: ${info.message}`;

if (info.context !== undefined) str += " " + logCtxToString(info.context);
if (info.context !== undefined && !isEmptyObject(info.context)) str += " " + logCtxToString(info.context);
if (info.error !== undefined) str += " - " + logCtxToString(info.error);

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

() => {
const error = new Error("err message");
error.stack = "$STACK";
return {
id: "regular log with error and metadata",
opts: {module: "test"},
message: "foo bar",
context: {meta: "data"},
error: error,
output: {
human: `[test] \u001b[33mwarn\u001b[39m: foo bar meta=data - err message\n${error.stack}`,
json: '{"context":{"meta":"data"},"error":{"message":"err message","stack":"$STACK"},"level":"warn","message":"foo bar","module":"test"}',
},
};
},
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export function toExpectedCase(
}
}

function isObjectObject(val: unknown): boolean {
function isObjectObject(val: unknown): val is object {
return val != null && typeof val === "object" && Array.isArray(val) === false;
}

export function isPlainObject(o: unknown): boolean {
export function isPlainObject(o: unknown): o is object {
if (isObjectObject(o) === false) return false;

// If has modified constructor
Expand All @@ -53,6 +53,10 @@ export function isPlainObject(o: unknown): boolean {
return true;
}

export function isEmptyObject(value: unknown): boolean {
return isObjectObject(value) && Object.keys(value).length === 0;
}

/**
* Creates an object with the same keys as object and values generated by running each own enumerable
* string keyed property of object thru iteratee.
Expand Down

0 comments on commit 4fa6327

Please sign in to comment.