Logit::Backend
Abstract base class for log output destinations.
Backends receive log events and write them to their destination (console, file, network, etc.). Each backend has:
- A minimum log level (events below this level are ignored)
- An optional formatter (converts events to strings)
- Namespace bindings (per-namespace level overrides)
Built-in Backends
Backend::Console- Writes to STDOUT with colorized human-readable outputBackend::File- Writes to a file with JSON output
Creating a Custom Backend
Subclass Backend and implement the log method:
class MyBackend < Logit::Backend
def initialize(name = "my_backend", level = Logit::LogLevel::Info)
super(name, level)
end
def log(event : Logit::Event) : Nil
return unless should_log?(event)
# Format and output the event
output = @formatter.try(&.format(event)) || event.to_json
# ... write output to your destination
end
def flush : Nil
# Flush any buffered data
end
def close : Nil
# Release resources
end
end
Namespace Bindings
Backends can have different log levels for different namespaces:
backend = Logit::Backend::Console.new
# Default level is Info, but Database classes log at Warn
backend.bind("MyApp::Database::*", Logit::LogLevel::Warn)
# Except QueryBuilder, which logs at Debug
backend.bind("MyApp::Database::QueryBuilder", Logit::LogLevel::Debug)
Constructors
Creates a new backend with the given name and level.
Instance methods
Binds a namespace pattern to a specific log level.
Events from classes matching the pattern will use this level instead of the backend's default level. More specific patterns take precedence.
Pattern syntax:
MyApp::*- matches classes directly in MyAppMyApp::**- matches classes in MyApp and all nested namespacesMyApp::Database::Query- matches exactly this class
Closes the backend and releases resources.
Override this if your backend holds resources (file handles, connections). The default implementation is a no-op.
Flushes any buffered data.
Override this if your backend buffers output. The default implementation is a no-op.
Logs an event to this backend.
Implementations should check should_log?(event) before processing.
Checks if this backend should log the given event.
Takes into account both the backend's level and any namespace bindings.