class Logit::Integrations::CrystalLogAdapter

Overview

Adapter that captures Crystal Log calls and forwards them to Logit.

This enables unified logging infrastructure where all logs flow through Logit, providing OTLP export, trace context, and wide events.

Installation

require "logit"
require "logit/integrations/crystal_log_adapter"

# Configure Logit first
Logit.configure do |config|
  config.console(Logit::LogLevel::Debug)
  config.otlp("http://localhost:4318/v1/logs")
end

# Install the adapter
Logit::Integrations::CrystalLogAdapter.install

# All Log.info/debug/etc calls now flow through Logit
Log.info { "This is captured by Logit" }

Protection Against Library Interference

Once installed, the adapter monkeypatches Log.setup, Log.setup_from_env, and Log::Builder#bind to automatically reinstall itself after any library attempts to reconfigure Crystal's logging. This ensures that all logs continue to flow through Logit even when third-party libraries call Log.setup_from_env at require time.

Trace Context Integration

When installed, Crystal Log calls automatically inherit trace context from any active Logit span:

@[Logit::Log]
def process_request
  # This Log call inherits the trace context from the span
  Log.info { "Processing request" }

  do_work

  Log.info { "Request complete" }
end

Namespace Mapping

Crystal Log sources are mapped to Logit namespaces, allowing you to control logging per-source:

Logit.configure do |config|
  console = config.console

  # Control Crystal Log sources via namespace bindings
  config.bind "db.*", Logit::LogLevel::Debug, console
  config.bind "http.client", Logit::LogLevel::Warn, console
end

Defined in:

logit/integrations/crystal_log_adapter.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(dispatch_mode : ::Log::DispatchMode = :async) #

[View source]

Class Method Detail

def self.install(dispatch_mode : ::Log::DispatchMode = :sync) : Nil #

Install this adapter as Crystal Log backend.

This replaces the default Log backend so all Log calls flow through Logit. You should configure Logit backends BEFORE calling install.

Once installed, the adapter will automatically reinstall itself if any library calls Log.setup, Log.setup_from_env, or modifies the Log builder directly. This protects against third-party libraries that configure logging at require time.

  • dispatch_mode: How to dispatch log entries. Use :sync for synchronous (safer but may block) or :async for asynchronous (better performance). Default is :sync for reliability.
# Configure Logit first
Logit.configure do |config|
  config.console(Logit::LogLevel::Debug)
end

# Then install the adapter
Logit::Integrations::CrystalLogAdapter.install

# Or with async dispatch for better performance
Logit::Integrations::CrystalLogAdapter.install(dispatch_mode: :async)

[View source]
def self.installed? : Bool #

Check if the adapter is currently installed.


[View source]
def self.intercepting? : Bool #

Check if we're currently in the middle of an intercept operation. Used by the monkeypatched methods to avoid infinite recursion.


[View source]
def self.reinstall_if_needed : Nil #

Reinstall the adapter after external code modified Log configuration. This is called by the monkeypatched Log methods.


[View source]
def self.uninstall : Nil #

Uninstall the adapter and restore default Log behavior.

This resets Crystal Log to its default configuration (Info to STDOUT) and removes the monkeypatches that protect against library interference.


[View source]

Instance Method Detail

def write(entry : ::Log::Entry) : Nil #
Description copied from class Log::Backend

Writes the entry to this backend.


[View source]