File size: 1,967 Bytes
4c76b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Runtime logging helpers route plugin runtime logs through OpenClaw verbosity controls.
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);
}

/** Creates the plugin runtime logging facade. */
export function createRuntimeLogging(): PluginRuntime["logging"] {
  return {
    shouldLogVerbose,
    getChildLogger: (bindings, opts) => {
      const overrideLevel = opts?.level ? normalizeLogLevel(opts.level) : undefined;
      const childOpts = overrideLevel ? { level: overrideLevel } : undefined;
      // Resolve the child logger per call: tslog snapshots a sublogger's min level at
      // creation, so a long-lived plugin logger (e.g. a channel monitor) would keep
      // dropping writes after the log level is raised at runtime even though
      // shouldLogVerbose() reports the new level. Skip the pre-gate when an override is
      // set since it may be more permissive than the current file level.
      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"),
      };
    },
  };
}