class Logit::Event::Attributes

Overview

Type-safe structured attribute storage for log events.

Attributes provide a way to attach arbitrary structured data to log events. Values are stored as JSON::Any for flexibility while maintaining type safety through the setter methods.

Setting Attributes

Use the type-specific #set methods for primitive types:

attrs = Logit::Event::Attributes.new

# Primitive types
attrs.set("user.name", "alice")
attrs.set("user.age", 30_i64)
attrs.set("request.latency", 0.042)
attrs.set("user.active", true)

Use #set_object and #set_array for complex structures:

attrs.set_object("user", name: "alice", role: "admin")
attrs.set_array("tags", "production", "critical")

Use #set_any for any JSON-serializable type:

attrs.set_any("config", my_config_object)

Getting Attributes

if value = attrs.get("user.name")
  puts value.as_s  # => "alice"
end

Defined in:

logit/events/attributes.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Creates a new empty Attributes instance.


[View source]

Instance Method Detail

def get(key : String) : JSON::Any | Nil #

Gets an attribute value, returning nil if not found or if the value is null.


[View source]
def get?(key : String) : JSON::Any | Nil #

Alias for #get.


[View source]
def set(key : String, value : String) : Nil #

Sets a string attribute.


[View source]
def set(key : String, value : Int32 | Int64) : Nil #

Sets an integer attribute.


[View source]
def set(key : String, value : Float32 | Float64) : Nil #

Sets a float attribute.


[View source]
def set(key : String, value : Bool) : Nil #

Sets a boolean attribute.


[View source]
def set(key : String, value : Nil) : Nil #

Sets a nil attribute.


[View source]
def set(key : String, value : Array(JSON::Any)) : Nil #

Sets an array attribute.


[View source]
def set(key : String, value : Hash(String, JSON::Any)) : Nil #

Sets a hash attribute.


[View source]
def set_any(key : String, value : _) : Nil #

Sets an attribute from any JSON-serializable value.

Use this for custom types that implement #to_json.


[View source]
def set_array(key : String, *values) : Nil #

Sets an array attribute from variadic arguments.

attrs.set_array("tags", "web", "api", "v2")
# Results in: {"tags": ["web", "api", "v2"]}

[View source]
def set_object(key : String, **values) : Nil #

Sets a nested object attribute from named arguments.

attrs.set_object("http", method: "POST", status: 200)
# Results in: {"http": {"method": "POST", "status": 200}}

[View source]
def to_json(json : JSON::Builder) : Nil #

[View source]
def to_json : String #

[View source]
def values : Hash(String, JSON::Any) #

The underlying attribute storage.


[View source]
def values=(values : Hash(String, JSON::Any)) #

The underlying attribute storage.


[View source]