abstract class Logit::Backend

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:

Built-in Backends

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(name : String, level : Logit::LogLevel = LogLevel::Info, formatter : Logit::Formatter | Nil = nil) #

Creates a new backend with the given name and level.


[View source]

Instance Method Detail

def bind(pattern : String, level : LogLevel) : Nil #

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 MyApp
  • MyApp::** - matches classes in MyApp and all nested namespaces
  • MyApp::Database::Query - matches exactly this class

[View source]
def bindings : Array(NamespaceBinding) #

Namespace-specific log level bindings.


[View source]
def bindings=(bindings : Array(NamespaceBinding)) #

Namespace-specific log level bindings.


[View source]
def close : Nil #

Closes the backend and releases resources.

Override this if your backend holds resources (file handles, connections). The default implementation is a no-op.


[View source]
def flush : Nil #

Flushes any buffered data.

Override this if your backend buffers output. The default implementation is a no-op.


[View source]
def formatter : Formatter | Nil #

Formatter used to convert events to strings.


[View source]
def formatter=(formatter : Formatter | Nil) #

Formatter used to convert events to strings.


[View source]
def level : LogLevel #

Minimum log level for this backend.


[View source]
def level=(level : LogLevel) #

Minimum log level for this backend.


[View source]
abstract def log(event : Event) : Nil #

Logs an event to this backend.

Implementations should check #should_log?(event) before processing.


[View source]
def name : String #

Unique name for this backend (used for removal and identification).


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

Unique name for this backend (used for removal and identification).


[View source]
def should_log?(event : Event) : Bool #

Checks if this backend should log the given event.

Takes into account both the backend's level and any namespace bindings.


[View source]
def should_log_level?(level : LogLevel, namespace : String) : Bool #

Checks if this backend would log at the given level for a namespace.

Used for early filtering before creating spans/events.


[View source]