Skip to content

Commit df8533d

Browse files
promptium-aiclaude
andcommitted
Add log dumping on program exit
- Add dumpLogsToConsole() function to output all logs when exiting - Register exit handlers for graceful shutdown, SIGINT, and SIGTERM - Dump logs to stderr to avoid interfering with normal program output - Handle both error logs and console logs with clear section headers 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ff9ed8f commit df8533d

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import {run} from './app.js';
2-
import {initializeFileLogging, logError} from './shared/utils/logger.js';
2+
import {initializeFileLogging, logError, dumpLogsToConsole} from './shared/utils/logger.js';
33

44
// Initialize file logging before running the app
55
initializeFileLogging();
66

7+
// Handle graceful shutdown and dump logs
8+
function handleExit() {
9+
dumpLogsToConsole();
10+
}
11+
12+
// Register exit handlers
13+
process.on('exit', handleExit);
14+
process.on('SIGINT', () => {
15+
handleExit();
16+
process.exit(0);
17+
});
18+
process.on('SIGTERM', () => {
19+
handleExit();
20+
process.exit(0);
21+
});
22+
723
run().catch((err) => {
824
logError('Application crashed', err?.stack || err);
25+
handleExit();
926
process.exit(1);
1027
});

src/shared/utils/logger.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,36 @@ export function clearLogs(): void {
153153
} catch (error) {
154154
// Silent fail
155155
}
156+
}
157+
158+
// Dump logs to console on exit
159+
export function dumpLogsToConsole(): void {
160+
try {
161+
originalConsoleError('\n=== ERROR LOGS ===');
162+
if (fs.existsSync(ERROR_LOG_FILE)) {
163+
const errorLogs = fs.readFileSync(ERROR_LOG_FILE, 'utf8');
164+
if (errorLogs.trim()) {
165+
originalConsoleError(errorLogs);
166+
} else {
167+
originalConsoleError('No error logs found.');
168+
}
169+
} else {
170+
originalConsoleError('No error log file found.');
171+
}
172+
173+
originalConsoleError('\n=== CONSOLE LOGS ===');
174+
if (fs.existsSync(CONSOLE_LOG_FILE)) {
175+
const consoleLogs = fs.readFileSync(CONSOLE_LOG_FILE, 'utf8');
176+
if (consoleLogs.trim()) {
177+
originalConsoleError(consoleLogs);
178+
} else {
179+
originalConsoleError('No console logs found.');
180+
}
181+
} else {
182+
originalConsoleError('No console log file found.');
183+
}
184+
originalConsoleError('=== END LOGS ===\n');
185+
} catch (error) {
186+
originalConsoleError('Failed to dump logs:', error);
187+
}
156188
}

0 commit comments

Comments
 (0)