UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.06 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{t as clearActiveProgressLine}from"./progress-line-CCGdmF_S.mjs";import{n as loggingState}from"./state-kXlBD9Q4.mjs";import{i as stripAnsi}from"./ansi-54CEWBZf.mjs";import{V as readLoggingConfig,c as redactSensitiveText}from"./redact-BT59pdZ_.mjs";import{t as isVerbose}from"./global-state-BYcIiONI.mjs";import{t as exitAfterSignalExitBarriers}from"./signal-exit-barrier-DoATIAtq.mjs";import{n as formatTimestamp}from"./timestamps-gtUruwBs.mjs";import{r as formatJsonConsoleLine}from"./json-console-line-C76Rgjbb.mjs";import{i as normalizeLogLevel}from"./levels-i0MxdVax.mjs";import{a as getLogger,m as resolveEnvLogLevelOverride}from"./logger-bB_ZfEX5.mjs";import util from"node:util"; //!src/logging/console.ts function normalizeConsoleLevel(e){if(isVerbose())return`debug`;if(!e&&process.env.VITEST===`true`&&process.env.OPENCLAW_TEST_CONSOLE!==`1`)return`silent`;return normalizeLogLevel(e,`info`)}function normalizeConsoleStyle(e){if(e===`compact`||e===`json`||e===`pretty`)return e;if(!process.stdout.isTTY)return`compact`;return`pretty`}function resolveConsoleSettings(){const e=resolveEnvLogLevelOverride();if(process.env.VITEST===`true`&&process.env.OPENCLAW_TEST_CONSOLE!==`1`&&!isVerbose()&&!e&&!loggingState.overrideSettings)return{level:`silent`,style:normalizeConsoleStyle(void 0)};const t=loggingState.overrideSettings??readLoggingConfig();return{level:e??normalizeConsoleLevel(t?.consoleLevel),style:normalizeConsoleStyle(t?.consoleStyle)}}function getConsoleSettings(){const e=loggingState.cachedConsoleSettings;if(e)return e;const t=resolveConsoleSettings();loggingState.cachedConsoleSettings=t;return t}function routeLogsToStderr(){loggingState.forceConsoleToStderr=true}function setConsoleSubsystemFilter(e){if(!e||e.length===0){loggingState.consoleSubsystemFilter=null;return}const t=e.map(e=>e.trim()).filter(e=>e.length>0);loggingState.consoleSubsystemFilter=t.length>0?t:null}async function withConsoleSubsystemsSuppressed(e){const t=loggingState.consoleSubsystemFilter?[...loggingState.consoleSubsystemFilter]:null;setConsoleSubsystemFilter([`__openclaw_tui_quiet__`]);try{return await e()}finally{setConsoleSubsystemFilter(t)}}function setConsoleTimestampPrefix(e){loggingState.consoleTimestampPrefix=e}function shouldLogSubsystemToConsole(e){const t=loggingState.consoleSubsystemFilter;if(!t||t.length===0)return true;const n=normalizeOptionalString(e);if(!n)return false;return t.some(e=>n===e||n.startsWith(`${e}/`))}const SUPPRESSED_CONSOLE_PREFIXES=[`Closing session:`,`Opening session:`,`Removing old closed session:`,`Session already closed`,`Session already open`];const NODE_PROCESS_WARNING_PREFIX=`(${process.release.name}:${process.pid}) `;function shouldSuppressConsoleMessage(e){return SUPPRESSED_CONSOLE_PREFIXES.some(t=>e.startsWith(t))}function brokenOutputReason(e){const t=e?.code;return t===`EPIPE`?`output pipe closed (EPIPE)`:t===`EIO`?`output I/O failed (EIO)`:void 0}function formatConsoleTimestamp(e){const t=/* @__PURE__ */ new Date;if(e===`pretty`)return formatTimestamp(t,{style:`short`}).replace(/[+-]\d{2}:\d{2}$/,``);return formatTimestamp(t,{style:`long`})}function captureConsoleTraceStack(e,t){const n=new Error(e);n.name=`Trace`;Error.captureStackTrace(n,t);return n.stack===void 0?`Trace: ${e}`:typeof n.stack===`string`?n.stack:util.format(n.stack)}function hasTimestampPrefix(e){return/^(?:\d{2}:\d{2}:\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?)/.test(e)}function writeFormattedConsoleOutput(e){const t=getConsoleSettings().style;const n=t!==`json`&&loggingState.consoleTimestampPrefix?stripAnsi(e.formatted).trimStart():``;const r=n&&!hasTimestampPrefix(n)?formatConsoleTimestamp(t):``;const i=e.level===`trace`&&e.caller?captureConsoleTraceStack(e.formatted,e.caller):void 0;try{const n=t===`json`?formatJsonConsoleLine({level:e.level,message:stripAnsi(e.formatted),meta:i===void 0?void 0:{stack:stripAnsi(i)}}):redactSensitiveText(i??e.formatted);const a=r?`${r} ${n}`:n;clearActiveProgressLine();if(loggingState.forceConsoleToStderr)process.stderr.write(`${a}\n`);else if(t!==`json`&&!r&&e.args.length===0&&i===void 0)e.write.apply(console,e.args);else e.write.call(console,a)}catch(e){if(brokenOutputReason(e))return;throw e}}function writeRootConsoleLine(e,t){const n=loggingState.rawConsole;if(!n)return false;if(shouldSuppressConsoleMessage(t))return true;writeFormattedConsoleOutput({level:e===`error`?`error`:`info`,args:[t],formatted:t,write:n[e]});return true}function enableConsoleCapture(){if(loggingState.consolePatched)return;loggingState.consolePatched=true;if(!loggingState.streamErrorHandlersInstalled){loggingState.streamErrorHandlersInstalled=true;for(const e of[process.stdout,process.stderr])e.on(`error`,e=>{const t=brokenOutputReason(e);if(t){exitAfterSignalExitBarriers(process.exitCode??0,t);return}throw e})}const e={log:console.log,info:console.info,warn:console.warn,error:console.error,debug:console.debug};loggingState.rawConsole={log:e.log,info:e.info,warn:e.warn,error:e.error};const forward=(e,t)=>{const forwardedConsoleCall=(...n)=>{const r=util.format(...n);let i=e;if(e===`error`&&r.startsWith(NODE_PROCESS_WARNING_PREFIX)){const e=util.getCallSites(2,{sourceMap:false})[1];if(e?.functionName===`writeOut`&&e.scriptName===`node:internal/process/warning`)i=`warn`}if(shouldSuppressConsoleMessage(r))return;try{getLogger()[i](r)}catch{}writeFormattedConsoleOutput({level:i,args:n,formatted:r,write:t,caller:forwardedConsoleCall})};return forwardedConsoleCall};console.log=forward(`info`,e.log);console.info=forward(`info`,e.info);console.warn=forward(`warn`,e.warn);console.error=forward(`error`,e.error);console.debug=forward(`debug`,e.debug);console.trace=forward(`trace`,e.error)}export{setConsoleSubsystemFilter as a,withConsoleSubsystemsSuppressed as c,routeLogsToStderr as i,writeRootConsoleLine as l,formatConsoleTimestamp as n,setConsoleTimestampPrefix as o,getConsoleSettings as r,shouldLogSubsystemToConsole as s,enableConsoleCapture as t};