Files
rea/src/logger.ts
T
Kaoru0822andClaude Opus 5.5 3159732597 fix(cli): report an invalid REA_LOG_LEVEL as configuration (#2140)
createCli parsed REA_LOG_LEVEL with a throwing schema, so an invalid or
uppercase value crashed every command, including doctor and --help, with
a ZodError stack trace. Parse it into a ConfigurationError result and have
the executable print the same configuration message as the MCP adapter
and exit 1 before building the CLI.

Fixes #2135

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 06:07:37 -07:00

43 lines
1.3 KiB
TypeScript

import pino, { type Logger } from "pino";
import { z } from "zod";
import { ConfigurationError } from "./domain/configurationErrors.js";
import { err, ok, type Result } from "./domain/result.js";
/** Supported structured log levels at the process boundary. */
export type LogLevel = z.infer<typeof logLevelSchema>;
const logLevelSchema = z
.enum(["trace", "debug", "info", "warn", "error", "fatal", "silent"])
.default("info");
/** Parse the optional REA_LOG_LEVEL environment variable. */
export const parseLogLevel = (
value: unknown,
): Result<LogLevel, ConfigurationError> => {
const parsed = logLevelSchema.safeParse(value);
if (parsed.success) return ok(parsed.data);
return err(
new ConfigurationError("Invalid REA environment configuration", {
cause: parsed.error,
settings: parsed.error.issues.map(({ message }) => ({
setting: "REA_LOG_LEVEL",
constraint: message,
})),
}),
);
};
/** Create a JSON logger on stderr so protocol and CLI result stdout stay valid. */
export const createLogger = (mode: "mcp" | "cli", level: LogLevel): Logger =>
pino(
{
level,
base: { application: "rea", mode },
},
pino.destination({ dest: 2, sync: false }),
);
/** Logger used by embedders that have not opted into process output. */
export const silentLogger = pino({ level: "silent" });