mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-30 01:06:11 +00:00
Feat/logger support log level validation0222 (#23436)
* 1、环境变量**:新增 `OPENCLAW_LOG_LEVEL`,可取值 `silent|fatal|error|warn|info|debug|trace`。设置后同时覆盖**文件日志**与**控制台**的级别,优先级高于配置文件。 2、启动参数**:在 `openclaw gateway run` 上新增 `--log-level <level>`,对该次进程同时生效于文件与控制台;未传时仍使用环境变量或配置文件。 * fix(logging): make log-level override global and precedence-safe --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import { escapeRegExp } from "../../utils.js";
|
||||
import { hasFlag, hasRootVersionAlias } from "../argv.js";
|
||||
import { formatCliBannerLine, hasEmittedCliBanner } from "../banner.js";
|
||||
import { replaceCliName, resolveCliName } from "../cli-name.js";
|
||||
import { CLI_LOG_LEVEL_VALUES, parseCliLogLevelOption } from "../log-level-option.js";
|
||||
import { getCoreCliCommandsWithSubcommands } from "./command-registry.js";
|
||||
import type { ProgramContext } from "./context.js";
|
||||
import { getSubCliCommandsWithSubcommands } from "./register.subclis.js";
|
||||
@@ -54,6 +55,11 @@ export function configureProgramHelp(program: Command, ctx: ProgramContext) {
|
||||
.option(
|
||||
"--profile <name>",
|
||||
"Use a named profile (isolates OPENCLAW_STATE_DIR/OPENCLAW_CONFIG_PATH under ~/.openclaw-<name>)",
|
||||
)
|
||||
.option(
|
||||
"--log-level <level>",
|
||||
`Global log level override for file + console (${CLI_LOG_LEVEL_VALUES})`,
|
||||
parseCliLogLevelOption,
|
||||
);
|
||||
|
||||
program.option("--no-color", "Disable ANSI colors", false);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Command } from "commander";
|
||||
import { setVerbose } from "../../globals.js";
|
||||
import { isTruthyEnvValue } from "../../infra/env.js";
|
||||
import type { LogLevel } from "../../logging/levels.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { getCommandPath, getVerboseFlag, hasHelpOrVersion } from "../argv.js";
|
||||
import { emitCliBanner } from "../banner.js";
|
||||
@@ -22,6 +23,26 @@ function setProcessTitleForCommand(actionCommand: Command) {
|
||||
// Commands that need channel plugins loaded
|
||||
const PLUGIN_REQUIRED_COMMANDS = new Set(["message", "channels", "directory"]);
|
||||
|
||||
function getRootCommand(command: Command): Command {
|
||||
let current = command;
|
||||
while (current.parent) {
|
||||
current = current.parent;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
function getCliLogLevel(actionCommand: Command): LogLevel | undefined {
|
||||
const root = getRootCommand(actionCommand);
|
||||
if (typeof root.getOptionValueSource !== "function") {
|
||||
return undefined;
|
||||
}
|
||||
if (root.getOptionValueSource("logLevel") !== "cli") {
|
||||
return undefined;
|
||||
}
|
||||
const logLevel = root.opts<Record<string, unknown>>().logLevel;
|
||||
return typeof logLevel === "string" ? (logLevel as LogLevel) : undefined;
|
||||
}
|
||||
|
||||
export function registerPreActionHooks(program: Command, programVersion: string) {
|
||||
program.hook("preAction", async (_thisCommand, actionCommand) => {
|
||||
setProcessTitleForCommand(actionCommand);
|
||||
@@ -40,6 +61,10 @@ export function registerPreActionHooks(program: Command, programVersion: string)
|
||||
}
|
||||
const verbose = getVerboseFlag(argv, { includeDebug: true });
|
||||
setVerbose(verbose);
|
||||
const cliLogLevel = getCliLogLevel(actionCommand);
|
||||
if (cliLogLevel) {
|
||||
process.env.OPENCLAW_LOG_LEVEL = cliLogLevel;
|
||||
}
|
||||
if (!verbose) {
|
||||
process.env.NODE_NO_WARNINGS ??= "1";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user