feat: colorize debug log screen output (#1365)

This commit is contained in:
jnjpng
2026-03-12 12:56:05 -06:00
committed by GitHub
parent e37e997b98
commit e94c15ea87

View File

@@ -37,7 +37,7 @@ function getDebugFile(): string | null {
} }
/** Print to screen (or LETTA_DEBUG_FILE). Only called when LETTA_DEBUG=1. */ /** Print to screen (or LETTA_DEBUG_FILE). Only called when LETTA_DEBUG=1. */
function printDebugLine(line: string): void { function printDebugLine(line: string, level: "log" | "warn" = "log"): void {
const debugFile = getDebugFile(); const debugFile = getDebugFile();
if (debugFile) { if (debugFile) {
try { try {
@@ -47,7 +47,11 @@ function printDebugLine(line: string): void {
// Fall back to console if file write fails // Fall back to console if file write fails
} }
} }
console.log(line.trimEnd()); const colored =
level === "warn"
? `\x1b[38;5;167m${line.trimEnd()}\x1b[0m` // muted red
: `\x1b[38;5;179m${line.trimEnd()}\x1b[0m`; // muted golden yellow
console.log(colored);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -148,6 +152,7 @@ function writeDebugLine(
prefix: string, prefix: string,
message: string, message: string,
args: unknown[], args: unknown[],
level: "log" | "warn" = "log",
): void { ): void {
const ts = new Date().toISOString(); const ts = new Date().toISOString();
const body = format(`[${prefix}] ${message}`, ...args); const body = format(`[${prefix}] ${message}`, ...args);
@@ -158,7 +163,7 @@ function writeDebugLine(
// Screen output only when LETTA_DEBUG is on // Screen output only when LETTA_DEBUG is on
if (isDebugEnabled()) { if (isDebugEnabled()) {
printDebugLine(line); printDebugLine(line, level);
} }
} }
@@ -187,5 +192,5 @@ export function debugWarn(
message: string, message: string,
...args: unknown[] ...args: unknown[]
): void { ): void {
writeDebugLine(prefix, `WARN: ${message}`, args); writeDebugLine(prefix, `WARN: ${message}`, args, "warn");
} }