class
Logit::Integrations::CrystalLogAdapter
- Logit::Integrations::CrystalLogAdapter
- Log::Backend
- Reference
- Object
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.crConstructors
Class Method Summary
-
.install(dispatch_mode : ::Log::DispatchMode = :sync) : Nil
Install this adapter as Crystal Log backend.
-
.installed? : Bool
Check if the adapter is currently installed.
-
.intercepting? : Bool
Check if we're currently in the middle of an intercept operation.
-
.reinstall_if_needed : Nil
Reinstall the adapter after external code modified Log configuration.
-
.uninstall : Nil
Uninstall the adapter and restore default Log behavior.
Instance Method Summary
-
#write(entry : ::Log::Entry) : Nil
Writes the entry to this backend.
Constructor Detail
Class Method Detail
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
:syncfor synchronous (safer but may block) or:asyncfor asynchronous (better performance). Default is:syncfor 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)
Check if we're currently in the middle of an intercept operation. Used by the monkeypatched methods to avoid infinite recursion.
Reinstall the adapter after external code modified Log configuration. This is called by the monkeypatched Log methods.
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.
Instance Method Detail
Writes the entry to this backend.