src — logging

Module: src-logging Cohesion: 0.80 Members: 0

src — logging

The src/logging module provides a comprehensive system for logging user interactions, AI responses, and tool executions. Its primary goal is to enable session replay, debugging, and analytics by capturing a complete history of each interaction session. This module is designed to be robust, automatically saving session data to disk and offering various methods for retrieval and management.

Core Concepts & Data Structures

The logging module revolves around a few key data structures that define the shape of a logged session:

The InteractionLogger Class

The InteractionLogger class is the central component of this module. It manages the lifecycle of a session, records interactions, and handles persistence to disk.

classDiagram
    direction LR
    class InteractionLogger {
        +startSession(options): string
        +endSession(): void
        +logMessage(message): void
        +logToolCalls(toolCalls): void
        +logToolResult(toolCallId, result): void
        +updateCost(cost): void
        +save(): void
        +getCurrentSession(): SessionData
        +static loadSession(id): SessionData
        +static listSessions(options?): {sessions: SessionMetadata[], total: number}
        +static deleteSession(sessionId): boolean
        +static formatSession(session): string
        +dispose(): void
    }
    class SessionData {
        <<struct>>
    }
    InteractionLogger ..> SessionData : manages/returns

Session Lifecycle Management

An InteractionLogger instance manages one active session at a time.

Logging Interactions

Once a session is started, various methods are available to record events:

All logging methods trigger a save() operation if autoSave is enabled, ensuring data is regularly persisted.

Data Persistence and Retrieval

The InteractionLogger handles saving and loading session data from the file system.

The module also provides static methods for managing sessions across the application:

Internal Helpers

Storage Mechanism

Session logs are stored in the user's home directory under ~/.codebuddy/logs. The directory structure is organized by date:

~/.codebuddy/logs/
├── YYYY-MM-DD/
│   ├── <session-id-1>.json
│   ├── <session-id-2>.json
│   └── ...
└── YYYY-MM-DD/
    ├── <session-id-N>.json
    └── ...

Each log file is a JSON representation of the SessionData interface, ensuring a consistent and easily parseable format. The version field in SessionData allows for future schema evolution.

Accessing the Logger

The module provides two ways to obtain an InteractionLogger instance:

Integration Points

The InteractionLogger is a foundational module, integrated across various parts of the codebase:

This module provides a robust and centralized mechanism for capturing and managing the rich history of interactions, crucial for debugging, analysis, and future features like session replay.