class Logit::Backend::Null

Overview

Null backend that discards all log events.

This is the default backend when Logit is first required, ensuring that libraries using Logit don't impose logging on applications. Applications can replace this with real backends by calling Logit.configure.

Inspired by Python's logging.NullHandler pattern, this allows libraries to use Logit for instrumentation without producing any output unless the consuming application explicitly enables it.

Usage in Libraries

Libraries can use Logit freely - events will be discarded by default:

# In your library shard
require "logit"

module MyLib
  def self.process(data : String) : String
    span = Logit::Span.new("mylib.process")
    span.attributes.set("input_size", data.size)
    span.end_time = Time.utc

    # This will be discarded unless app configures a real backend
    Logit::Tracer.default.emit(span.to_event(
      trace_id: span.trace_id,
      level: Logit::LogLevel::Info,
      code_file: __FILE__,
      code_line: __LINE__,
      method_name: "process",
      class_name: "MyLib"
    ))

    data.upcase
  end
end

Enabling Library Logs in Applications

Applications can enable logging from libraries by configuring Logit:

require "logit"

# Configure Logit to enable output
Logit.configure do |config|
  config.console(Logit::LogLevel::Debug)
  config.bind "MyLib::**", LogLevel::Debug, _
end

require "my-lib"

# Now library logs will appear
MyLib.process("hello")  # -> [DEBUG] ... mylib.process ...

Defined in:

logit/backends/null.cr

Constructors

Instance Method Summary

Instance methods inherited from class Logit::Backend

bind(pattern : String, level : LogLevel) : Nil bind, bindings : Array(NamespaceBinding) bindings, bindings=(bindings : Array(NamespaceBinding)) bindings=, close : Nil close, flush : Nil flush, formatter : Formatter | Nil formatter, formatter=(formatter : Formatter | Nil) formatter=, level : LogLevel level, level=(level : LogLevel) level=, log(event : Event) : Nil log, name : String name, name=(name : String) name=, should_log?(event : Event) : Bool should_log?, should_log_level?(level : LogLevel, namespace : String) : Bool should_log_level?

Constructor methods inherited from class Logit::Backend

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

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def close : Nil #

No-op - nothing to close.


[View source]
def flush : Nil #

No-op - nothing to flush.


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

No-op - discards all events.


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

Always returns false - never logs anything.


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

Always returns false - never logs anything at any level.


[View source]