| |
| import { shouldLogVerbose } from "../../globals.js"; |
| import { getChildLogger, isFileLogLevelEnabled } from "../../logging.js"; |
| import { normalizeLogLevel } from "../../logging/levels.js"; |
| import type { PluginRuntime } from "./types.js"; |
|
|
| type RuntimeLogMethod = "debug" | "info" | "warn" | "error"; |
|
|
| export function writeRuntimeLog( |
| logger: Pick<ReturnType<typeof getChildLogger>, RuntimeLogMethod>, |
| level: RuntimeLogMethod, |
| message: string, |
| meta?: Record<string, unknown>, |
| ): void { |
| if (meta && Object.keys(meta).length > 0) { |
| logger[level](meta, message); |
| return; |
| } |
| logger[level](message); |
| } |
|
|
| |
| export function createRuntimeLogging(): PluginRuntime["logging"] { |
| return { |
| shouldLogVerbose, |
| getChildLogger: (bindings, opts) => { |
| const overrideLevel = opts?.level ? normalizeLogLevel(opts.level) : undefined; |
| const childOpts = overrideLevel ? { level: overrideLevel } : undefined; |
| |
| |
| |
| |
| |
| const emit = |
| (level: RuntimeLogMethod) => (message: string, meta?: Record<string, unknown>) => { |
| if (!overrideLevel && !isFileLogLevelEnabled(level)) { |
| return; |
| } |
| const logger = getChildLogger(bindings, childOpts); |
| writeRuntimeLog(logger, level, message, meta); |
| }; |
| return { |
| debug: emit("debug"), |
| info: emit("info"), |
| warn: emit("warn"), |
| error: emit("error"), |
| }; |
| }, |
| }; |
| } |
|
|