mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-14 11:12:17 +03:00
* test(opencode-plugin): cover configured log levels * fix(opencode-plugin): respect lifecycle log level * fix(opencode-plugin): isolate lifecycle loggers --------- Co-authored-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
83 lines
2.8 KiB
TypeScript
83 lines
2.8 KiB
TypeScript
/**
|
|
* Structured logger for the OmniRoute plugin.
|
|
*
|
|
* Levels: error < warn < info < debug
|
|
* Default: warn (matches current console.warn behavior)
|
|
* Set via features.logLevel in plugin options.
|
|
*/
|
|
|
|
export type LogLevel = "error" | "warn" | "info" | "debug";
|
|
|
|
const LEVEL_ORDER: Record<LogLevel, number> = {
|
|
error: 0,
|
|
warn: 1,
|
|
info: 2,
|
|
debug: 3,
|
|
};
|
|
|
|
const TAG = "[omniroute-plugin]";
|
|
|
|
function shouldLog(current: LogLevel, target: LogLevel): boolean {
|
|
return LEVEL_ORDER[current] >= LEVEL_ORDER[target];
|
|
}
|
|
|
|
let _level: LogLevel = "warn";
|
|
|
|
export function setLogLevel(level: LogLevel): void {
|
|
_level = level;
|
|
}
|
|
|
|
export function getLogLevel(): LogLevel {
|
|
return _level;
|
|
}
|
|
|
|
function fmt(level: LogLevel, msg: string, tag?: string): string {
|
|
const prefix = tag ? `${TAG}${tag}` : TAG;
|
|
return `${prefix} [${level.toUpperCase()}] ${msg}`;
|
|
}
|
|
|
|
function buildLogger(getLevel: () => LogLevel) {
|
|
return {
|
|
error(msg: string, ...args: unknown[]): void {
|
|
if (shouldLog(getLevel(), "error")) console.error(fmt("error", msg), ...args);
|
|
},
|
|
warn(msg: string, ...args: unknown[]): void {
|
|
if (shouldLog(getLevel(), "warn")) console.warn(fmt("warn", msg), ...args);
|
|
},
|
|
info(msg: string, ...args: unknown[]): void {
|
|
if (shouldLog(getLevel(), "info")) console.warn(fmt("info", msg), ...args);
|
|
},
|
|
debug(msg: string, ...args: unknown[]): void {
|
|
if (shouldLog(getLevel(), "debug")) console.warn(fmt("debug", msg), ...args);
|
|
},
|
|
/** Always emit regardless of level (for critical init breadcrumbs). */
|
|
always(msg: string, ...args: unknown[]): void {
|
|
console.warn(TAG, msg, ...args);
|
|
},
|
|
|
|
// ── Tagged child loggers ────────────────────────────────────────────
|
|
child(tag: string) {
|
|
return {
|
|
error: (msg: string, ...args: unknown[]) =>
|
|
shouldLog(getLevel(), "error") && console.error(fmt("error", msg, tag), ...args),
|
|
warn: (msg: string, ...args: unknown[]) =>
|
|
shouldLog(getLevel(), "warn") && console.warn(fmt("warn", msg, tag), ...args),
|
|
info: (msg: string, ...args: unknown[]) =>
|
|
shouldLog(getLevel(), "info") && console.warn(fmt("info", msg, tag), ...args),
|
|
debug: (msg: string, ...args: unknown[]) =>
|
|
shouldLog(getLevel(), "debug") && console.warn(fmt("debug", msg, tag), ...args),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
export type Logger = ReturnType<typeof buildLogger>;
|
|
|
|
/** Create an instance-scoped logger whose level cannot be changed by other plugin instances. */
|
|
export function createLogger(level: LogLevel): Logger {
|
|
return buildLogger(() => level);
|
|
}
|
|
|
|
/** Backward-compatible module-global logger controlled by setLogLevel(). */
|
|
export const logger: Logger = buildLogger(() => _level);
|