logger

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2025 License: Apache-2.0 Imports: 6 Imported by: 6

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

Constants

This section is empty.

Variables

View Source
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

func APIRequest(provider, method, url string, headers map[string]string, body interface{})

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

func APIResponse(provider string, statusCode int, body string, err error)

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

func Debug(msg string, args ...any)

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

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a debug message with context and structured attributes.

func Error

func Error(msg string, args ...any)

Error logs an error message with structured attributes. Use for errors that affect operation but don't cause complete failure.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs an error message with context and structured attributes.

func Info

func Info(msg string, args ...any)

Info logs an informational message with structured key-value attributes. Args should be provided in key-value pairs: key1, value1, key2, value2, ...

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs an informational message with context and structured attributes. The context can be used for request tracing and cancellation.

func LLMCall

func LLMCall(provider, role string, messages int, temperature float64, attrs ...any)

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 LLMError

func LLMError(provider, role string, err error, attrs ...any)

LLMError logs an LLM API error for debugging and monitoring.

func LLMResponse

func LLMResponse(provider, role string, tokensIn, tokensOut int, cost float64, attrs ...any)

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

func RedactSensitiveData(input string) string

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

func SetLevel(level slog.Level)

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

func ToolCall(provider string, messages, tools int, choice string, attrs ...any)

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

func ToolResponse(provider string, tokensIn, tokensOut, toolCalls int, cost float64, attrs ...any)

ToolResponse logs the result of tool executions with token usage and cost.

func Warn

func Warn(msg string, args ...any)

Warn logs a warning message with structured attributes. Use for recoverable errors or unexpected but non-critical situations.

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs a warning message with context and structured attributes.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL