ExLLM.Infrastructure.Logger (ex_llm v0.8.1)

View Source

Unified logging for ExLLM with automatic context and security features.

This module provides a simple, unified logging interface that combines the simplicity of Elixir's Logger with ExLLM-specific features like context tracking, security redaction, and component filtering.

Usage

Simple logging (like Elixir's Logger):

ExLLM.Infrastructure.Logger.info("Model loaded successfully")
ExLLM.Infrastructure.Logger.error("Failed to connect", error: reason)
ExLLM.Infrastructure.Logger.debug("Processing chunk", size: byte_size(data))

With provider context:

ExLLM.Infrastructure.Logger.with_context(provider: :openai, operation: :chat) do
  ExLLM.Infrastructure.Logger.info("Starting chat request")
  # ... do work ...
  ExLLM.Infrastructure.Logger.info("Chat completed", tokens: 150)
end

Structured logging for specific events:

ExLLM.Infrastructure.Logger.log_request(:openai, url, body, headers)
ExLLM.Infrastructure.Logger.log_response(:openai, response, duration_ms)
ExLLM.Infrastructure.Logger.log_retry(:anthropic, attempt, max_attempts, reason)

Configuration

config :ex_llm,
  log_level: :info,  # :debug, :info, :warn, :error, :none
  log_components: %{
    requests: true,
    responses: true,
    streaming: false,
    retries: true,
    cache: false,
    models: true
  },
  log_redaction: %{
    api_keys: true,
    content: false
  }

Summary

Functions

Clear all context.

Log a debug message with optional metadata.

Log an error message with optional metadata.

Log an info message with optional metadata.

Log an API request with automatic redaction.

Add context that persists for the rest of the process.

Log a warning message with optional metadata.

Log a warning message with optional metadata. Alias for warn/1 to maintain compatibility with modern Elixir Logger API.

Set context for all logs within the given function.

Functions

clear_context()

Clear all context.

debug(message, metadata \\ [])

Log a debug message with optional metadata.

error(message, metadata \\ [])

Log an error message with optional metadata.

info(message, metadata \\ [])

Log an info message with optional metadata.

log_cache_event(event, key, data \\ %{})

Log a cache event.

log_model_event(provider, event, data \\ %{})

Log a model event.

log_request(provider, url, body, headers \\ [])

Log an API request with automatic redaction.

log_response(provider, response, duration_ms)

Log an API response.

log_retry(provider, attempt, max_attempts, reason, delay_ms)

Log a retry attempt.

log_stream_event(provider, event, data \\ %{})

Log a streaming event.

put_context(context)

Add context that persists for the rest of the process.

warn(message, metadata \\ [])

Log a warning message with optional metadata.

warning(message, metadata \\ [])

Log a warning message with optional metadata. Alias for warn/1 to maintain compatibility with modern Elixir Logger API.

with_context(context, fun)

Set context for all logs within the given function.

Examples

ExLLM.Infrastructure.Logger.with_context(provider: :openai, operation: :chat) do
  ExLLM.Infrastructure.Logger.info("Starting request")
  # All logs in here will include provider and operation
end