/** * 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 = { 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; /** 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);