mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 23:40:45 +00:00
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
import { danger, info, logVerboseConsole, success, warn } from "./globals.js";
|
|
import { getLogger } from "./logging/logger.js";
|
|
import { createSubsystemLogger } from "./logging/subsystem.js";
|
|
import { defaultRuntime, type RuntimeEnv } from "./runtime.js";
|
|
|
|
const subsystemPrefixRe = /^([a-z][a-z0-9-]{1,20}):\s+(.*)$/i;
|
|
|
|
function splitSubsystem(message: string) {
|
|
const match = message.match(subsystemPrefixRe);
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
const [, subsystem, rest] = match;
|
|
return { subsystem, rest };
|
|
}
|
|
|
|
type LogMethod = "info" | "warn" | "error";
|
|
type RuntimeMethod = "log" | "error";
|
|
|
|
function logWithSubsystem(params: {
|
|
message: string;
|
|
runtime: RuntimeEnv;
|
|
runtimeMethod: RuntimeMethod;
|
|
runtimeFormatter: (value: string) => string;
|
|
loggerMethod: LogMethod;
|
|
subsystemMethod: LogMethod;
|
|
}) {
|
|
const parsed = params.runtime === defaultRuntime ? splitSubsystem(params.message) : null;
|
|
if (parsed) {
|
|
createSubsystemLogger(parsed.subsystem)[params.subsystemMethod](parsed.rest);
|
|
return;
|
|
}
|
|
params.runtime[params.runtimeMethod](params.runtimeFormatter(params.message));
|
|
getLogger()[params.loggerMethod](params.message);
|
|
}
|
|
|
|
export function logInfo(message: string, runtime: RuntimeEnv = defaultRuntime) {
|
|
logWithSubsystem({
|
|
message,
|
|
runtime,
|
|
runtimeMethod: "log",
|
|
runtimeFormatter: info,
|
|
loggerMethod: "info",
|
|
subsystemMethod: "info",
|
|
});
|
|
}
|
|
|
|
export function logWarn(message: string, runtime: RuntimeEnv = defaultRuntime) {
|
|
logWithSubsystem({
|
|
message,
|
|
runtime,
|
|
runtimeMethod: "log",
|
|
runtimeFormatter: warn,
|
|
loggerMethod: "warn",
|
|
subsystemMethod: "warn",
|
|
});
|
|
}
|
|
|
|
export function logSuccess(message: string, runtime: RuntimeEnv = defaultRuntime) {
|
|
logWithSubsystem({
|
|
message,
|
|
runtime,
|
|
runtimeMethod: "log",
|
|
runtimeFormatter: success,
|
|
loggerMethod: "info",
|
|
subsystemMethod: "info",
|
|
});
|
|
}
|
|
|
|
export function logError(message: string, runtime: RuntimeEnv = defaultRuntime) {
|
|
logWithSubsystem({
|
|
message,
|
|
runtime,
|
|
runtimeMethod: "error",
|
|
runtimeFormatter: danger,
|
|
loggerMethod: "error",
|
|
subsystemMethod: "error",
|
|
});
|
|
}
|
|
|
|
export function logDebug(message: string) {
|
|
// Always emit to file logger (level-filtered); console only when verbose.
|
|
getLogger().debug(message);
|
|
logVerboseConsole(message);
|
|
}
|