Logit
Logit - Annotation-based logging with OpenTelemetry support for Crystal.
Logit provides automatic method instrumentation through annotations, structured
logging with wide events, and full OpenTelemetry compatibility. Simply annotate
methods with @[Logit::Log] and Logit handles the rest.
Quick Start
require "logit"
# Configure Logit (optional - defaults to console output)
Logit.configure do |config|
config.console(level: Logit::LogLevel::Debug)
end
class MyService
@[Logit::Log]
def process(data : String) : String
data.upcase
end
end
Annotation Options
The @[Logit::Log] annotation supports the following options:
log_args(Bool) - Whether to log method arguments (default: true, or LOG_ARGS_DEFAULT if defined)log_return(Bool) - Whether to log return values (default: true, or LOG_RETURN_DEFAULT if defined)log_exception(Bool) - Whether to log exceptions (default: true, or LOG_EXCEPTION_DEFAULT if defined)name(String) - Custom span name (default: method name)level(LogLevel) - Log level for this method (default: Info)redact(Array(String)) - Argument names to redact from logs
Key Features
- Automatic instrumentation: No manual log calls needed
- OpenTelemetry semantics: Trace IDs, span IDs, and semantic attributes
- Fiber-aware context: Spans are tracked per-fiber for safe concurrency
- Flexible backends: Console, file, or custom backends
- Namespace filtering: Control log levels per namespace pattern
- Redaction support: Automatically redact sensitive data
See Logit.configure for configuration options and Logit::Backend for
available output backends.
Constants
Class methods
Adds method-local context that is cleared after the current method completes.
@[Logit::Log]
def process(item : Item) : Bool
Logit.add_context(item_type: item.type)
# ... context is included in this method's log event
true
end # context cleared here
Adds fiber-local context from a named tuple.
Adds fiber-local context that persists across method calls in this fiber.
Use this for request-scoped data like request IDs or user information.
# At the start of a request
Logit.add_fiber_context(request_id: request.id, user_id: current_user.id)
# All logs in this fiber now include request_id and user_id
process_request
save_data
# At the end of the request
Logit.clear_fiber_context
Configures Logit with the provided block and applies the configuration.
This is the main entry point for setting up Logit. The configuration is applied immediately after the block completes.
Logit.configure do |config|
config.console(level: Logit::LogLevel::Debug)
config.file("logs/app.log")
config.redact_common_patterns
end
If you don't call configure, Logit uses a default console backend at
Info level.
Logs an exception at a specific level.
Logs an exception with full details at Error level.
Macros
Execute a block with additional context that is automatically removed after
Setup macro to be called at the END of a class definition (for manual instrumentation)
This generates wrapper methods for all @Log annotated methods
Note: In most cases, you should rely on the global macro finished hook instead
Nested types
- Logit::API
- Logit::Backend
- Logit::BufferedIO
- Logit::Config
- Logit::Context
- Logit::Event
- Logit::ExceptionInfo
- Logit::Formatter
- Logit::Instrumentation
- Logit::Integrations
- Logit::Log
- Logit::LogLevel
- Logit::Macros
- Logit::NamespaceBinding
- Logit::PatternMatcher
- Logit::Redaction
- Logit::Span
- Logit::SpanEvent
- Logit::Status
- Logit::Tracer
- Logit::Utils