logfire client for zig
at c7a46726fe7ff8bb65e4e72c985aa146fbf6d404 66 lines 1.7 kB view raw
1//! basic logfire example 2//! 3//! demonstrates spans, logging, metrics, and export to logfire. 4//! 5//! run with: 6//! LOGFIRE_TOKEN=your_token zig build example 7//! 8//! or without token for console-only output: 9//! zig build example 10 11const std = @import("std"); 12const logfire = @import("logfire"); 13 14pub fn main() !void { 15 // configure logfire 16 // will send to logfire if LOGFIRE_TOKEN is set, otherwise just prints 17 const lf = try logfire.configure(.{ 18 .service_name = "logfire-zig-example", 19 .service_version = "0.1.0", 20 .environment = "development", 21 }); 22 defer lf.shutdown(); 23 24 // simple logging 25 logfire.info("application started", .{}); 26 logfire.debug("debug message with value: {d}", .{42}); 27 28 // span with timing 29 { 30 const s = logfire.span("example.work", .{ 31 .iteration = @as(i64, 1), 32 }); 33 defer s.end(); 34 35 // simulate some work 36 std.posix.nanosleep(0, 10 * std.time.ns_per_ms); 37 38 logfire.info("work completed", .{}); 39 } 40 41 // nested spans 42 { 43 const outer = logfire.span("example.outer", .{}); 44 defer outer.end(); 45 46 { 47 const inner = logfire.span("example.inner", .{ 48 .depth = @as(i64, 1), 49 }); 50 defer inner.end(); 51 52 std.posix.nanosleep(0, 5 * std.time.ns_per_ms); 53 } 54 } 55 56 // metrics 57 logfire.counter("requests.total", 1); 58 logfire.counter("requests.total", 1); 59 logfire.gaugeInt("connections.active", 42); 60 logfire.gaugeDouble("cpu.usage", 0.75); 61 62 // flush to ensure all data is sent 63 try lf.flush(); 64 65 logfire.info("example complete", .{}); 66}