API
module
Defined in src/logit/api.cr:75
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:
Instance Methods
#debug(message : String, **kwargs) : Nil
Logs a message at Debug level.
#debug
Logs a message at Debug level with lazy evaluation.
#error(message : String, **kwargs) : Nil
Logs a message at Error level.
#error
Logs a message at Error level with lazy evaluation.
#exception(message : String, ex : Exception, level : LogLevel, **kwargs) : Nil
Logs an exception at a specific level.
#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.
#fatal(message : String, **kwargs) : Nil
Logs a message at Fatal level.
#fatal
Logs a message at Fatal level with lazy evaluation.
#info(message : String, **kwargs) : Nil
Logs a message at Info level.
#info
Logs a message at Info level with lazy evaluation.
#trace(message : String, **kwargs) : Nil
Logs a message at Trace level.
Supports both string and block (lazy) evaluation:
#trace
Logs a message at Trace level with lazy evaluation.
The block is only executed if trace logging is enabled.
#warn(message : String, **kwargs) : Nil
Logs a message at Warn level.
#warn
Logs a message at Warn level with lazy evaluation.