Files
openidea/src/ui/logColors.ts
T
Qingandqtvbwfn ce67c7e129 feat(run): colour console log lines by level (#2924)
Run and Debug consoles can colour whole lines by detected log level (ERROR, WARN, INFO, DEBUG, TRACE), reusing the log editor mode's level vocabulary; ANSI colours the program prints win. Settings › Build, Execution, Deployment › Console has the toggle and one colour per level (scheme tokens by default). Off by default, since IDEA's console doesn't colour lines by level.

Co-authored-by: qtvbwfn <5848026+qtvbwfn@users.noreply.github.com>
2026-11-12 19:01:01 +08:00

54 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Console log-line colours by level (Settings › Build, Execution, Deployment ›
* Console). Detection mirrors the `log` editor mode (`editor/core/cm/log.ts`):
* explicit `level=`/`severity=` fields and `[warn]`/`<Info>` markers match in
* any case, bare words only in upper case, so prose stays plain.
*/
import { LEVELS } from "@/editor/core/cm/log";
export type LogLevel = "error" | "warn" | "info" | "debug" | "trace";
/** Explicit level fields: JSON `"level":"info"`, logfmt `level=error`, GCP `severity`. */
const KEY_RE = new RegExp(`(?:^|[^A-Za-z0-9_])(?:level|lvl|severity)["']?\\s*[:=]\\s*["']?(${LEVELS})\\b`, "i");
/** Bracketed markers: Maven `[ERROR]`, log4j2 `<Info>`. */
const MARK_RE = new RegExp(`[\\[<](${LEVELS})[\\]>]`, "i");
/** Bare upper-case words at a word start (the `log` mode's rule). */
const BARE_RE = new RegExp(`(?:^|[^A-Za-z0-9_$.-])(${LEVELS})(?![A-Za-z0-9_])`);
const ALIAS: Record<string, LogLevel> = {
FATAL: "error",
CRITICAL: "error",
SEVERE: "error",
EMERG: "error",
ALERT: "error",
ERROR: "error",
ERR: "error",
WARNING: "warn",
WARN: "warn",
NOTICE: "warn",
INFO: "info",
DEBUG: "debug",
VERBOSE: "trace",
TRACE: "trace",
FINEST: "trace",
FINER: "trace",
FINE: "trace",
};
function detectLogLevel(text: string): LogLevel | null {
// An explicit declaration is the most trusted, then a marker, then a bare word.
const m = KEY_RE.exec(text) ?? MARK_RE.exec(text) ?? BARE_RE.exec(text);
return m ? (ALIAS[m[1]!.toUpperCase()] ?? null) : null;
}
/** A line's colour, or `undefined`: colouring off, a stream the IDE owns, or no level. */
export function logLineColor(
text: string,
kind: string,
colors: { enabled: boolean } & Record<LogLevel, string>,
): string | undefined {
if (!colors.enabled || (kind !== "stdout" && kind !== "stderr")) return undefined;
const level = detectLogLevel(text);
return level ? colors[level] : undefined;
}