Documentation
¶
Overview ¶
Package logger provides structured logging with automatic PII redaction.
This package wraps Go's standard log/slog with convenience functions for:
- LLM API call logging (requests, responses, errors)
- Tool execution logging
- Automatic API key and sensitive data redaction
- Contextual logging with request tracing
- Level-based verbosity control
All exported functions use the global DefaultLogger which can be configured for different output formats and log levels.
Index ¶
- Variables
- func APIRequest(provider, method, url string, headers map[string]string, body interface{})
- func APIResponse(provider string, statusCode int, body string, err error)
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func LLMCall(provider, role string, messages int, temperature float64, attrs ...any)
- func LLMError(provider, role string, err error, attrs ...any)
- func LLMResponse(provider, role string, tokensIn, tokensOut int, cost float64, attrs ...any)
- func RedactSensitiveData(input string) string
- func SetLevel(level slog.Level)
- func SetVerbose(verbose bool)
- func ToolCall(provider string, messages, tools int, choice string, attrs ...any)
- func ToolResponse(provider string, tokensIn, tokensOut, toolCalls int, cost float64, ...)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultLogger is the global structured logger instance. // It is safe for concurrent use and initialized with slog.LevelInfo by default. DefaultLogger *slog.Logger )
Functions ¶
func APIRequest ¶
APIRequest logs HTTP API request details at debug level with automatic PII redaction. This function is a no-op when debug logging is disabled for performance.
Parameters:
- provider: The API provider name (e.g., "OpenAI", "Anthropic")
- method: HTTP method (GET, POST, etc.)
- url: Request URL (will be redacted for sensitive data)
- headers: HTTP headers map (will be redacted)
- body: Request body (will be marshaled to JSON and redacted)
Sensitive data in URL, headers, and body are automatically redacted.
func APIResponse ¶
APIResponse logs HTTP API response details at debug level with automatic PII redaction. This function is a no-op when debug logging is disabled for performance.
Parameters:
- provider: The API provider name
- statusCode: HTTP status code
- body: Response body as string (will be redacted)
- err: Error if the request failed (takes precedence over body logging)
Response bodies are attempted to be parsed as JSON for pretty formatting. Status codes are logged with emoji indicators: 🟢 (2xx), 🟡 (3xx), 🔴 (4xx/5xx).
func Debug ¶
Debug logs a debug-level message with structured attributes. Debug messages are only output when the log level is set to LevelDebug or lower.
func DebugContext ¶
DebugContext logs a debug message with context and structured attributes.
func Error ¶
Error logs an error message with structured attributes. Use for errors that affect operation but don't cause complete failure.
func ErrorContext ¶
ErrorContext logs an error message with context and structured attributes.
func Info ¶
Info logs an informational message with structured key-value attributes. Args should be provided in key-value pairs: key1, value1, key2, value2, ...
func InfoContext ¶
InfoContext logs an informational message with context and structured attributes. The context can be used for request tracing and cancellation.
func LLMCall ¶
LLMCall logs an LLM API call with structured fields for observability. Additional attributes can be passed as key-value pairs after the required parameters.
func LLMResponse ¶
LLMResponse logs an LLM API response with token usage and cost tracking. Cost should be provided in USD (e.g., 0.0001 for $0.0001).
func RedactSensitiveData ¶
RedactSensitiveData removes API keys and other sensitive information from strings. It replaces matched patterns with a redacted form that preserves the first few characters for debugging while hiding the sensitive portion.
Supported patterns:
- OpenAI keys (sk-...): Shows first 4 chars
- Google keys (AIza...): Shows first 4 chars
- Bearer tokens: Shows only "Bearer [REDACTED]"
This function is safe for concurrent use as it only reads from the compiled patterns.
func SetLevel ¶
SetLevel changes the logging level for all subsequent log operations. This is safe for concurrent use as it replaces the entire logger instance.
func SetVerbose ¶
func SetVerbose(verbose bool)
SetVerbose enables debug-level logging when verbose is true, otherwise sets info-level. This is a convenience wrapper around SetLevel for command-line verbose flags.
func ToolCall ¶
ToolCall logs a tool execution request with context about available tools. The choice parameter indicates the tool selection mode (e.g., "auto", "required", "none").
func ToolResponse ¶
ToolResponse logs the result of tool executions with token usage and cost.
Types ¶
This section is empty.