logfire client for zig
at c7a46726fe7ff8bb65e4e72c985aa146fbf6d404 44 lines 1.2 kB view raw
1//! structured logging 2//! 3//! logs are exported as OTLP log records with severity levels. 4 5const std = @import("std"); 6pub const Level = @import("config.zig").Config.Level; 7const Attribute = @import("attribute.zig").Attribute; 8 9pub const LogRecord = struct { 10 timestamp_ns: i128, 11 level: Level, 12 message: []const u8, 13 trace_id: ?[16]u8, 14 attributes: [max_attributes]Attribute = undefined, 15 attribute_count: usize = 0, 16 17 pub const max_attributes = 32; 18 19 pub fn init( 20 trace_id: ?[16]u8, 21 level: Level, 22 message: []const u8, 23 attrs: anytype, 24 ) LogRecord { 25 var record = LogRecord{ 26 .timestamp_ns = std.time.nanoTimestamp(), 27 .level = level, 28 .message = message, 29 .trace_id = trace_id, 30 }; 31 32 record.attribute_count = Attribute.fromStruct(attrs, &record.attributes); 33 34 return record; 35 } 36}; 37 38// tests 39 40test "LogRecord init" { 41 const record = LogRecord.init(null, .info, "test message", .{}); 42 try std.testing.expectEqual(Level.info, record.level); 43 try std.testing.expectEqualStrings("test message", record.message); 44}