chore(logging): strip redundant console prefixes
This commit is contained in:
@@ -470,6 +470,26 @@ function formatSubsystemForConsole(subsystem: string): string {
|
|||||||
return parts.join("/");
|
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: {
|
function formatConsoleLine(opts: {
|
||||||
level: Level;
|
level: Level;
|
||||||
subsystem: string;
|
subsystem: string;
|
||||||
@@ -501,13 +521,17 @@ function formatConsoleLine(opts: {
|
|||||||
: opts.level === "debug" || opts.level === "trace"
|
: opts.level === "debug" || opts.level === "trace"
|
||||||
? color.gray
|
? color.gray
|
||||||
: color.cyan;
|
: color.cyan;
|
||||||
|
const displayMessage = stripRedundantSubsystemPrefixForConsole(
|
||||||
|
opts.message,
|
||||||
|
displaySubsystem,
|
||||||
|
);
|
||||||
const time =
|
const time =
|
||||||
opts.style === "pretty"
|
opts.style === "pretty"
|
||||||
? color.gray(new Date().toISOString().slice(11, 19))
|
? color.gray(new Date().toISOString().slice(11, 19))
|
||||||
: "";
|
: "";
|
||||||
const prefixToken = prefixColor(prefix);
|
const prefixToken = prefixColor(prefix);
|
||||||
const head = [time, prefixToken].filter(Boolean).join(" ");
|
const head = [time, prefixToken].filter(Boolean).join(" ");
|
||||||
return `${head} ${levelColor(opts.message)}`;
|
return `${head} ${levelColor(displayMessage)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeConsoleLine(level: Level, line: string) {
|
function writeConsoleLine(level: Level, line: string) {
|
||||||
|
|||||||
26
src/logging/console-prefix.test.ts
Normal file
26
src/logging/console-prefix.test.ts
Normal 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");
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user