From 4fa6327bfc1ff41d0981e7ed4d3be99bf030cad1 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Wed, 18 Oct 2023 15:58:34 +0200 Subject: [PATCH] fix: remove extra white space in logs if context is empty (#6046) --- packages/logger/src/utils/format.ts | 3 ++- .../logger/test/fixtures/loggerFormats.ts | 19 ++++++++++++++++++- packages/utils/src/objects.ts | 8 ++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/logger/src/utils/format.ts b/packages/logger/src/utils/format.ts index 96e5893b71a0..21e2521c5796 100644 --- a/packages/logger/src/utils/format.ts +++ b/packages/logger/src/utils/format.ts @@ -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"; @@ -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; diff --git a/packages/logger/test/fixtures/loggerFormats.ts b/packages/logger/test/fixtures/loggerFormats.ts index 1138cd7cb3de..fffaaf9ea2f0 100644 --- a/packages/logger/test/fixtures/loggerFormats.ts +++ b/packages/logger/test/fixtures/loggerFormats.ts @@ -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"}', }, }; }, diff --git a/packages/utils/src/objects.ts b/packages/utils/src/objects.ts index 6a04c6385276..67d360a6b0c2 100644 --- a/packages/utils/src/objects.ts +++ b/packages/utils/src/objects.ts @@ -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 @@ -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.