struct

Logit::Event

Inherits Struct / Value / Object

A structured log event with OpenTelemetry-compatible fields.

Events are the core data structure passed to backends for logging. Each event contains:

  • Trace context (trace_id, span_id, parent_span_id)
  • Timing information (timestamp, duration)
  • Source location (file, line, method, class)
  • Structured attributes
  • Exception information (if applicable)

Events are created automatically by the instrumentation system. You typically interact with events through the Span API or by implementing custom formatters/backends.

OpenTelemetry Semantic Conventions

Events provide helper methods for setting OpenTelemetry semantic attributes:

if span = Logit::Span.current?
  # HTTP attributes
  span.attributes.set("http.method", "POST")
  span.attributes.set("http.route", "/api/users")
  span.attributes.set("http.status_code", 200_i64)

  # Database attributes
  span.attributes.set("db.system", "postgresql")
  span.attributes.set("db.statement", "SELECT * FROM users")
end

JSON Serialization

Events serialize to JSON in an OpenTelemetry-compatible format:

{
  "trace_id": "abc123...",
  "span_id": "def456...",
  "timestamp": "2024-01-15T10:30:00.000000Z",
  "duration_ms": 42,
  "name": "find_user",
  "level": "info",
  "code": {
    "file": "user_service.cr",
    "line": 15,
    "function": "find_user",
    "namespace": "UserService"
  },
  "attributes": { ... }
}

Constructors

new(trace_id : String, span_id : String, name : String, level : Logit::LogLevel, code_file : String, code_line : Int32, method_name : String, class_name : String, parent_span_id : Nil | String = nil)

Creates a new event with the given parameters.

Source

Instance methods

attributes

Structured attributes attached to this event.

Source
attributes=(attributes : Event::Attributes)

Structured attributes attached to this event.

Source
class_name

Fully-qualified class name containing the instrumented method.

Source
class_name=(class_name : String)

Fully-qualified class name containing the instrumented method.

Source
code_file

Source file where the instrumented method is defined.

Source
code_file=(code_file : String)

Source file where the instrumented method is defined.

Source
code_line

Line number where the instrumented method is defined.

Source
code_line=(code_line : Int32)

Line number where the instrumented method is defined.

Source
duration_ms

Duration of the operation in milliseconds.

Source
duration_ms=(duration_ms : Int64)

Duration of the operation in milliseconds.

Source
exception

Exception information if an error occurred.

Source
exception=(exception : ExceptionInfo | Nil)

Exception information if an error occurred.

Source
level

Log level of this event.

Source
level=(level : LogLevel)

Log level of this event.

Source
method_name

Name of the instrumented method.

Source
method_name=(method_name : String)

Name of the instrumented method.

Source
name

Name of this event (typically the method name).

Source
name=(name : String)

Name of this event (typically the method name).

Source
parent_span_id

Span ID of the parent span, or nil if this is a root span.

Source
parent_span_id=(parent_span_id : String | Nil)

Span ID of the parent span, or nil if this is a root span.

Source
set_code_function(function : String) : Nil

Code attributes

Source
set_code_namespace(namespace : String) : Nil
Source
set_db_name(name : String) : Nil
Source
set_db_operation(operation : String) : Nil
Source
set_db_statement(statement : String) : Nil
Source
set_db_system(system : String) : Nil

Database attributes

Source
set_exception_message(message : String) : Nil
Source
set_exception_type(type : String) : Nil

Exception attributes

Source
set_http_method(method : String) : Nil

Sets the HTTP request method (e.g., "GET", "POST").

Source
set_http_request_body_size(size : Int64) : Nil
Source
set_http_route(route : String) : Nil
Source
set_http_status_code(code : Int32) : Nil
Source
set_service_name(name : String) : Nil

Service attributes

Source
set_service_version(version : String) : Nil
Source
set_user_id(id : String | Int64) : Nil

User attributes

Source
set_user_role(role : String) : Nil
Source
span_events

Span events that occurred during the operation.

These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.

Source
span_events=(span_events : Array(Logit::SpanEvent))

Span events that occurred during the operation.

These are intermediate logs attached to the span, similar to OpenTelemetry's Span Events.

Source
span_id

Unique identifier for the span that generated this event.

Source
span_id=(span_id : String)

Unique identifier for the span that generated this event.

Source
status

Status of the operation (Ok or Error).

Source
status=(status : Status)

Status of the operation (Ok or Error).

Source
timestamp

When this event was created.

Source
timestamp=(timestamp : Time)

When this event was created.

Source
to_json(json : JSON::Builder) : Nil

Serialize to JSON

Source
to_json
Source
trace_id

W3C trace ID (128-bit hex string) shared across all spans in a trace.

Source
trace_id=(trace_id : String)

W3C trace ID (128-bit hex string) shared across all spans in a trace.

Source

Nested types