Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove extra white space in logs if context is empty #6046

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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