Files
openclaw/src/logging/levels.ts
zhumengzhu f6bf8c7202 fix(logging): correct levelToMinLevel mapping and related filter logic for tslog v4 (#44646)
* fix: correct levelToMinLevel mapping and isFileLogLevelEnabled direction for tslog v4

* test: add regression tests for logging level filter and child logger inheritance

* fix: propagate minLevel to toPinoLikeLogger sub-loggers

* fix: correct shouldLogToConsole comparison direction in subsystem.ts

* test: cover logging threshold regressions

* fix(logging): treat silent as non-emittable level

---------

Co-authored-by: Altay <altay@uinaf.dev>
2026-04-08 00:19:20 +01:00

39 lines
1.0 KiB
TypeScript

export const ALLOWED_LOG_LEVELS = [
"silent",
"fatal",
"error",
"warn",
"info",
"debug",
"trace",
] as const;
export type LogLevel = (typeof ALLOWED_LOG_LEVELS)[number];
export function tryParseLogLevel(level?: string): LogLevel | undefined {
if (typeof level !== "string") {
return undefined;
}
const candidate = level.trim();
return ALLOWED_LOG_LEVELS.includes(candidate as LogLevel) ? (candidate as LogLevel) : undefined;
}
export function normalizeLogLevel(level?: string, fallback: LogLevel = "info") {
return tryParseLogLevel(level) ?? fallback;
}
export function levelToMinLevel(level: LogLevel): number {
// tslog v4 logLevelId (src/index.ts): silly=0, trace=1, debug=2, info=3, warn=4, error=5, fatal=6
// tslog filters: logLevelId < minLevel is dropped, so higher minLevel = more restrictive.
const map: Record<LogLevel, number> = {
trace: 1,
debug: 2,
info: 3,
warn: 4,
error: 5,
fatal: 6,
silent: Number.POSITIVE_INFINITY,
};
return map[level];
}