Skip to content

Commit 21de2be

Browse files
committed
feat(logging): suppress error/warn logs during graceful shutdown
Add global shutdown flag to prevent error/warn logs from being emitted during graceful shutdown. This provides cleaner output when handling SIGINT/Ctrl+C signals.
1 parent d1fac7b commit 21de2be

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/agents/monitoring/cleanup.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AgentMonitorService } from './monitor.js';
22
import { AgentLoggerService } from './logger.js';
33
import * as logger from '../../shared/logging/logger.js';
4+
import { setShuttingDown } from '../../shared/logging/logger.js';
45
import { killAllActiveProcesses } from '../../infra/process/spawn.js';
56

67
/**
@@ -132,6 +133,9 @@ export class MonitoringCleanup {
132133
// Second Ctrl+C (after debounce): Stop workflow and exit
133134
logger.debug(`[${source}] Second Ctrl+C detected after ${timeSinceFirst}ms - stopping workflow and exiting`);
134135

136+
// Suppress all error/warn logs during graceful shutdown
137+
setShuttingDown(true);
138+
135139
// Emit workflow:skip to abort the currently running step
136140
(process as NodeJS.EventEmitter).emit('workflow:skip');
137141

@@ -153,6 +157,9 @@ export class MonitoringCleanup {
153157
private static async handleSignal(signal: string, message: string): Promise<void> {
154158
logger.debug(`Received ${signal}: ${message}`);
155159

160+
// Suppress all error/warn logs during graceful shutdown
161+
setShuttingDown(true);
162+
156163
// Kill all active child processes before cleanup
157164
logger.debug('Killing all active child processes...');
158165
killAllActiveProcesses();

src/shared/logging/logger.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ type LogLevel = keyof typeof LOG_LEVELS;
1717

1818
let debugLogStream: fs.WriteStream | null = null;
1919

20+
/**
21+
* Global shutdown flag - when true, error/warn logs are suppressed
22+
* Set this when handling SIGINT/Ctrl+C for clean exit
23+
*/
24+
let isShuttingDown = false;
25+
26+
export function setShuttingDown(value: boolean): void {
27+
isShuttingDown = value;
28+
}
29+
30+
export function getShuttingDown(): boolean {
31+
return isShuttingDown;
32+
}
33+
2034
function resolveRequestedLevel(): string {
2135
const explicit = process.env.LOG_LEVEL;
2236
if (explicit && explicit.trim()) {
@@ -80,6 +94,7 @@ export function info(message: string, ...args: unknown[]): void {
8094
}
8195

8296
export function warn(message: string, ...args: unknown[]): void {
97+
if (isShuttingDown) return; // Suppress during graceful shutdown
8398
if (shouldLog('warn')) {
8499
// Write directly to stderr to bypass console hijacking
85100
const formatted = formatMessage(`[WARN] ${message}`, ...args);
@@ -88,6 +103,7 @@ export function warn(message: string, ...args: unknown[]): void {
88103
}
89104

90105
export function error(message: string, ...args: unknown[]): void {
106+
if (isShuttingDown) return; // Suppress during graceful shutdown
91107
if (shouldLog('error')) {
92108
// Write directly to stderr to bypass console hijacking
93109
const formatted = formatMessage(`[ERROR] ${message}`, ...args);

0 commit comments

Comments
 (0)