From 62bdbe18219b40477a22563363461a289b5b3323 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 13 Jan 2026 04:27:28 +0000 Subject: [PATCH] chore(logging): strip redundant console prefixes --- src/logging.ts | 26 +++++++++++++++++++++++++- src/logging/console-prefix.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/logging/console-prefix.test.ts diff --git a/src/logging.ts b/src/logging.ts index 96e9a0fc1..9651e0cf2 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -470,6 +470,26 @@ function formatSubsystemForConsole(subsystem: string): string { return parts.join("/"); } +export function stripRedundantSubsystemPrefixForConsole( + message: string, + displaySubsystem: string, +): string { + if (!displaySubsystem) return message; + if (!message.startsWith(displaySubsystem)) return message; + + const next = message.slice( + displaySubsystem.length, + displaySubsystem.length + 1, + ); + if (next !== ":" && next !== " ") return message; + + let i = displaySubsystem.length; + while (message[i] === " ") i += 1; + if (message[i] === ":") i += 1; + while (message[i] === " ") i += 1; + return message.slice(i); +} + function formatConsoleLine(opts: { level: Level; subsystem: string; @@ -501,13 +521,17 @@ function formatConsoleLine(opts: { : opts.level === "debug" || opts.level === "trace" ? color.gray : color.cyan; + const displayMessage = stripRedundantSubsystemPrefixForConsole( + opts.message, + displaySubsystem, + ); const time = opts.style === "pretty" ? color.gray(new Date().toISOString().slice(11, 19)) : ""; const prefixToken = prefixColor(prefix); const head = [time, prefixToken].filter(Boolean).join(" "); - return `${head} ${levelColor(opts.message)}`; + return `${head} ${levelColor(displayMessage)}`; } function writeConsoleLine(level: Level, line: string) { diff --git a/src/logging/console-prefix.test.ts b/src/logging/console-prefix.test.ts new file mode 100644 index 000000000..dbb62a5eb --- /dev/null +++ b/src/logging/console-prefix.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from "vitest"; + +import { stripRedundantSubsystemPrefixForConsole } from "../logging.js"; + +describe("stripRedundantSubsystemPrefixForConsole", () => { + it("drops ':' prefix", () => { + expect( + stripRedundantSubsystemPrefixForConsole("discord: hello", "discord"), + ).toBe("hello"); + }); + + it("drops ' ' prefix", () => { + expect( + stripRedundantSubsystemPrefixForConsole( + "discord gateway: closed", + "discord", + ), + ).toBe("gateway: closed"); + }); + + it("keeps messages that do not start with the subsystem", () => { + expect( + stripRedundantSubsystemPrefixForConsole("discordant: hello", "discord"), + ).toBe("discordant: hello"); + }); +});