The logging server provides asynchronous, thread-safe logging capabilities for the Anthropic client. It manages log file creation, writing, and cleanup through a channel-based architecture.
type LogServer struct {
mu sync.Mutex // Synchronization for thread safety
file *os.File // Current log file handle
enabled bool // Logging state flag
logChan chan string // Message buffer channel
done chan struct{} // Shutdown synchronization
bufSize int // Channel buffer size
}
- Default buffer size: 1000 messages
- Log directory:
./logs
- File naming:
anthropic-debug-YYYYMMDD-HHMMSS.log
- Configurable via options pattern:
WithBufferSize()
- Mutex-protected file operations
- Single writer goroutine design
- Synchronized shutdown process
select {
case s.logChan <- text:
// Message sent successfully
default:
// Channel full, message dropped with warning
}
- Automatic session markers
- Timestamped entries
- Clean shutdown handling
// Default configuration
InitLogServer()
// Custom buffer size
InitLogServer(WithBufferSize(2000))
// Start logging
EnableLogging()
// Write logs
WriteLogs("Operation started")
// Check status
if IsLoggingEnabled() {
// Logging-dependent code
}
// Stop logging
DisableLogging()
=== Session Started: 2024-12-09 15:04:05 ===
[2024-12-09 15:04:05.123] Log message 1
[2024-12-09 15:04:06.234] Log message 2
=== Session Ended: 2024-12-09 15:04:07 ===
- Directory creation failures
- File operation errors
- Channel overflow conditions
- Buffer size impacts memory usage
- Non-blocking writes prevent application slowdown
- Single writer eliminates contention
- Mutex scope minimized for better concurrency
- Initialize early in application lifecycle
- Handle EnableLogging errors
- Call DisableLogging before shutdown
- Monitor channel overflow warnings
- Use appropriate buffer sizes for workload