chore(logging): strip redundant console prefixes

This commit is contained in:
Peter Steinberger
2026-01-13 04:27:28 +00:00
parent 58d1d11762
commit 62bdbe1821
2 changed files with 51 additions and 1 deletions

View File

@@ -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) {

View File

@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { stripRedundantSubsystemPrefixForConsole } from "../logging.js";
describe("stripRedundantSubsystemPrefixForConsole", () => {
it("drops '<subsystem>:' prefix", () => {
expect(
stripRedundantSubsystemPrefixForConsole("discord: hello", "discord"),
).toBe("hello");
});
it("drops '<subsystem> ' 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");
});
});