abstract class
Logit::Backend
- Logit::Backend
- Reference
- Object
Overview
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)
Direct Known Subclasses
Defined in:
logit/backend.crConstructors
-
.new(name : String, level : Logit::LogLevel = LogLevel::Info, formatter : Logit::Formatter | Nil = nil)
Creates a new backend with the given name and level.
Instance Method Summary
-
#bind(pattern : String, level : LogLevel) : Nil
Binds a namespace pattern to a specific log level.
-
#bindings : Array(NamespaceBinding)
Namespace-specific log level bindings.
-
#bindings=(bindings : Array(NamespaceBinding))
Namespace-specific log level bindings.
-
#close : Nil
Closes the backend and releases resources.
-
#flush : Nil
Flushes any buffered data.
-
#formatter : Formatter | Nil
Formatter used to convert events to strings.
-
#formatter=(formatter : Formatter | Nil)
Formatter used to convert events to strings.
-
#level : LogLevel
Minimum log level for this backend.
-
#level=(level : LogLevel)
Minimum log level for this backend.
-
#log(event : Event) : Nil
Logs an event to this backend.
-
#name : String
Unique name for this backend (used for removal and identification).
-
#name=(name : String)
Unique name for this backend (used for removal and identification).
-
#should_log?(event : Event) : Bool
Checks if this backend should log the given event.
-
#should_log_level?(level : LogLevel, namespace : String) : Bool
Checks if this backend would log at the given level for a namespace.
Constructor Detail
Creates a new backend with the given name and level.
Instance Method Detail
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.
Formatter used to convert events to strings.
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.
Checks if this backend would log at the given level for a namespace.
Used for early filtering before creating spans/events.