module Logit::API

Overview

Direct logging methods for libraries and manual instrumentation.

These methods provide a Crystal Log-like API for targeted logging when annotations aren't appropriate (e.g., libraries, ORMs, HTTP clients).

Lazy Evaluation

Like Crystal's Log library, these methods support lazy evaluation via blocks:

# Eager evaluation - string always computed
Logit.debug("Processing #{expensive_operation()}")

# Lazy evaluation - block only executed if logging is enabled
Logit.debug { "Processing #{expensive_operation()}" }

Trace Context Integration

Manual log calls automatically inherit trace context from any active span:

@[Logit::Log]
def process_order(order_id : Int64)
  # This log call inherits the trace_id and span_id from the annotation
  Logit.info { "Starting order processing" }

  validate_order(order_id)

  Logit.info { "Order validation complete" }
end

Usage in Libraries

module MyORM
  def self.execute_query(sql : String) : Array(Result)
    # Lazy debug log - only computed if debug enabled
    Logit.debug { "Executing query: #{sql}" }

    results = DB.query(sql)

    # Lazy info log with structured data
    Logit.info { "Query returned #{results.size} rows" }

    results
  rescue ex : DB::Error
    # Log exceptions with full context
    Logit.exception("Database query failed", ex)
    raise ex
  end
end

Structured Attributes

You can add structured attributes to log messages:

Logit.warn("Slow query detected",
  duration_ms: 450,
  query: sql,
  table: "users"
)

Extended Modules

Defined in:

logit/api.cr

Instance Method Summary

Instance Method Detail

def debug(message : String, **kwargs) : Nil #

Logs a message at Debug level.


[View source]
def debug(**kwargs, &block : -> String) : Nil #

Logs a message at Debug level with lazy evaluation.


[View source]
def error(message : String, **kwargs) : Nil #

Logs a message at Error level.


[View source]
def error(**kwargs, &block : -> String) : Nil #

Logs a message at Error level with lazy evaluation.


[View source]
def exception(message : String, ex : Exception, level : LogLevel, **kwargs) : Nil #

Logs an exception at a specific level.


[View source]
def exception(message : String, ex : Exception, **kwargs) : Nil #

Logs an exception with full details.

Creates an event at Error level with exception information including type, message, and stacktrace.

begin
  risky_operation
rescue ex : SomeError
  Logit.exception("Operation failed", ex)
  raise ex
end

[View source]
def fatal(message : String, **kwargs) : Nil #

Logs a message at Fatal level.


[View source]
def fatal(**kwargs, &block : -> String) : Nil #

Logs a message at Fatal level with lazy evaluation.


[View source]
def info(message : String, **kwargs) : Nil #

Logs a message at Info level.


[View source]
def info(**kwargs, &block : -> String) : Nil #

Logs a message at Info level with lazy evaluation.


[View source]
def trace(message : String, **kwargs) : Nil #

Logs a message at Trace level.

Supports both string and block (lazy) evaluation:

Logit.trace("Direct message")
Logit.trace { "Lazy message: #{expensive()}" }

[View source]
def trace(**kwargs, &block : -> String) : Nil #

Logs a message at Trace level with lazy evaluation.

The block is only executed if trace logging is enabled.


[View source]
def warn(message : String, **kwargs) : Nil #

Logs a message at Warn level.


[View source]
def warn(**kwargs, &block : -> String) : Nil #

Logs a message at Warn level with lazy evaluation.


[View source]