class Logit::Config

Overview

Configuration builder for setting up Logit logging infrastructure.

Use Logit.configure to create and apply a configuration. The config provides a fluent API for adding backends, setting up namespace filtering, and configuring redaction patterns.

Basic Configuration

Logit.configure do |config|
  config.console(level: Logit::LogLevel::Debug)
end

Multiple Backends

Logit.configure do |config|
  # Console for development
  console = config.console(level: Logit::LogLevel::Info)

  # JSON file for production logs
  file = config.file("logs/app.log", level: Logit::LogLevel::Debug)

  # Different levels per namespace
  config.bind("MyApp::Database::*", Logit::LogLevel::Warn, console)
  config.bind("MyApp::Http::*", Logit::LogLevel::Debug, file)
end

Redaction

Logit.configure do |config|
  config.console

  # Enable common security patterns (password, token, api_key, etc.)
  config.redact_common_patterns

  # Add custom patterns
  config.redact_patterns(/ssn/i, /credit_card/i)
end

Defined in:

logit/config.cr

Constructors

Instance Method Summary

Constructor Detail

def self.configure(&) : Config #

Creates a new Config, yields it for configuration, and returns it. Typically you should use Logit.configure instead, which also applies the configuration.


[View source]
def self.new #

[View source]

Instance Method Detail

def add_backend(backend : Backend) : Nil #

Adds a backend to the default tracer.

Creates the default tracer if it doesn't exist. For most applications, use #console or #file methods instead, which call this internally.


[View source]
def add_tracer(name : String, tracer : Tracer) : Nil #

Registers a named tracer.

Most applications only need the default tracer, but you can create additional named tracers for advanced use cases like multi-tenant logging.


[View source]
def bind(pattern : String, level : LogLevel, backend : Backend) : Nil #

Binds a namespace pattern to a log level for a specific backend.

This allows fine-grained control over which namespaces (classes) log at which levels. More specific patterns take precedence over less specific ones.

Pattern syntax:

  • MyApp::* - matches any class directly in MyApp
  • MyApp::** - matches any class in MyApp or any nested namespace
  • MyApp::Http::* - matches classes in MyApp::Http
console = config.console

# Only log warnings and above from database classes
config.bind("MyApp::Database::*", Logit::LogLevel::Warn, console)

# But log everything from the query builder
config.bind("MyApp::Database::QueryBuilder", Logit::LogLevel::Debug, console)

[View source]
def build : Nil #

Finalizes the configuration by setting the default tracer. Called automatically by Logit.configure.


[View source]
def console(level = LogLevel::Info, formatter = Formatter::Human.new, buffered : Bool = false) : Backend::Console #

Adds a console backend that writes to STDOUT.

The console backend uses Formatter::Human by default, which produces colorized, human-readable output suitable for development.

  • level: Minimum log level (default: Info)
  • formatter: Output formatter (default: Human)
  • buffered: Whether to buffer output (default: false for immediate display)

Returns the created backend for further configuration (e.g., namespace bindings).

config.console(level: Logit::LogLevel::Debug)

[View source]
def default_tracer_name : String #

Name of the default tracer used by instrumented methods.


[View source]
def default_tracer_name=(default_tracer_name : String) #

Name of the default tracer used by instrumented methods.


[View source]
def file(path : String, level = LogLevel::Info, buffered : Bool = true) : Backend::File #

Adds a file backend that writes to the specified path.

The file backend uses Formatter::JSON by default, which produces structured JSON output suitable for log aggregation systems.

  • path: Path to the log file (will be created if it doesn't exist)
  • level: Minimum log level (default: Info)
  • buffered: Whether to buffer output (default: true for performance)

Returns the created backend for further configuration (e.g., namespace bindings).

config.file("logs/app.log", level: Logit::LogLevel::Debug)

NOTE The file is opened with mode 0o600 (owner read/write only) by default. Symlinks are not followed unless explicitly enabled in Backend::File.


[View source]
def otlp(endpoint : String, level = LogLevel::Info, batch_size : Int32 = 512, flush_interval : Time::Span = 5.seconds, headers : Hash(String, String) = {} of String => String, timeout : Time::Span = 30.seconds, resource_attributes : Hash(String, String) = {} of String => String) : Backend::OTLP #

Adds an OTLP backend that exports logs to an OpenTelemetry collector.

The OTLP backend sends log events in batches via HTTP/JSON to an OpenTelemetry-compatible endpoint. Events are buffered and flushed when the batch size is reached or the flush interval elapses.

  • endpoint: OTLP HTTP endpoint URL (e.g., "http://localhost:4318/v1/logs")
  • level: Minimum log level (default: Info)
  • batch_size: Maximum events per batch (default: 512)
  • flush_interval: Time between automatic flushes (default: 5.seconds)
  • headers: HTTP headers for authentication (default: empty)
  • timeout: HTTP request timeout (default: 30.seconds)
  • resource_attributes: Resource attributes like service.name (default: empty)

Returns the created backend for further configuration (e.g., namespace bindings).

Logit.configure do |config|
  config.otlp(
    "http://localhost:4318/v1/logs",
    resource_attributes: {"service.name" => "my-app"}
  )
end

With authentication:

Logit.configure do |config|
  config.otlp(
    "https://otlp.example.com/v1/logs",
    headers: {"Authorization" => "Bearer token"},
    resource_attributes: {
      "service.name" => "my-app",
      "deployment.environment" => "production"
    }
  )
end

[View source]
def redact_common_patterns : Nil #

Enables a set of common security-related redaction patterns.

This is a convenience method that adds patterns for commonly sensitive argument names including: password, secret, token, api_key, auth, credential, private_key, access_key, and bearer.

config.redact_common_patterns

[View source]
def redact_patterns(*patterns : Regex) : Nil #

Adds global redaction patterns that apply to all instrumented methods.

Any argument name matching one of these regex patterns will have its value replaced with [REDACTED] in the logs.

config.redact_patterns(/ssn/i, /credit_card/i, /social_security/i)

[View source]
def tracers : Hash(String, Tracer) #

Registered tracers by name.


[View source]
def tracers=(tracers : Hash(String, Tracer)) #

Registered tracers by name.


[View source]