feat(logging): Implement structured logging across the application

- Introduced a new Logger utility to standardize logging with correlation IDs and structured context.
- Replaced console.error and console.log statements with logger methods in various modules including save.ts, summary.ts, parser.ts, HooksDatabase.ts, and worker-service.ts.
- Enhanced error handling and logging for better traceability of observations and summaries.
- Made observations.text nullable in the database schema to support structured fields.
- Added correlation IDs for tracking observations through the processing pipeline.
This commit is contained in:
Alex Newman
2025-10-18 19:15:52 -04:00
parent 874815770a
commit 05f3889deb
12 changed files with 757 additions and 140 deletions
+16 -3
View File
@@ -1,5 +1,6 @@
import { HooksDatabase } from '../services/sqlite/HooksDatabase.js';
import { createHookResponse } from './hook-response.js';
import { logger } from '../utils/logger.js';
export interface StopInput {
session_id: string;
@@ -28,7 +29,7 @@ export async function summaryHook(input?: StopInput): Promise<void> {
if (!session.worker_port) {
db.close();
console.error('[summary-hook] No worker port for session', session.id);
logger.error('HOOK', 'No worker port for session', { sessionId: session.id });
console.log(createHookResponse('Stop', true));
return;
}
@@ -38,6 +39,12 @@ export async function summaryHook(input?: StopInput): Promise<void> {
db.close();
try {
logger.dataIn('HOOK', 'Stop: Requesting summary', {
sessionId: session.id,
workerPort: session.worker_port,
promptNumber
});
const response = await fetch(`http://127.0.0.1:${session.worker_port}/sessions/${session.id}/summarize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -46,10 +53,16 @@ export async function summaryHook(input?: StopInput): Promise<void> {
});
if (!response.ok) {
console.error('[summary-hook] Failed to generate summary:', await response.text());
const errorText = await response.text();
logger.failure('HOOK', 'Failed to generate summary', {
sessionId: session.id,
status: response.status
}, errorText);
} else {
logger.debug('HOOK', 'Summary request sent successfully', { sessionId: session.id });
}
} catch (error: any) {
console.error('[summary-hook] Error:', error.message);
logger.failure('HOOK', 'Error requesting summary', { sessionId: session.id }, error);
} finally {
console.log(createHookResponse('Stop', true));
}