{"id":13344034,"url":"https://github.com/candrewlee14/zlog","last_synced_at":"2025-04-12T03:24:32.399Z","repository":{"id":38122626,"uuid":"479164355","full_name":"candrewlee14/zlog","owner":"candrewlee14","description":"A zero-allocation log library for Zig","archived":false,"fork":false,"pushed_at":"2022-09-25T01:29:12.000Z","size":43,"stargazers_count":58,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-25T23:01:39.468Z","etag":null,"topics":["logging","zig","ziglang"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/candrewlee14.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-07T22:15:31.000Z","updated_at":"2024-12-01T13:56:45.000Z","dependencies_parsed_at":"2023-01-17T17:49:30.625Z","dependency_job_id":null,"html_url":"https://github.com/candrewlee14/zlog","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candrewlee14%2Fzlog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candrewlee14%2Fzlog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candrewlee14%2Fzlog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candrewlee14%2Fzlog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/candrewlee14","download_url":"https://codeload.github.com/candrewlee14/zlog/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248511066,"owners_count":21116345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["logging","zig","ziglang"],"created_at":"2024-07-29T19:32:19.754Z","updated_at":"2025-04-12T03:24:32.377Z","avatar_url":"https://github.com/candrewlee14.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zlog - Zero-Allocation Logging\nA [zerolog](https://github.com/rs/zerolog)-inspired log library for Zig.\n\n![Pretty Logging Image](prettyLog.png)\n\n## Features\n - Blazing fast\n - Zero allocations\n - Leveled logging\n - Contextual logging \n - JSON, Plain, and Pretty logging formats\n\n## Getting Started\n\nCopy `zlog` folder to a `libs` subdirectory of the root of your project.\nThen in your `build.zig` add:\n```zig\nconst std = @import(\"std\");\nconst zlog = @import(\"libs/zlog/build.zig\");\n\npub fn build(b: *std.build.Builder) void {\n    ...\n    exe.addPackage(zlog.pkg);\n}\n```\nNow you can import and use `zlog`!\n\n### Simple Logging Example\nFor simple logging, import a global logger\n```zig\nconst zlog = @import(\"zlog\");\nconst log = \u0026zlog.json_logger;\n// You could also use pretty_logger, plain_logger \n\npub fn main() anyerror!void {\n    try log.print(\"Hello!\");\n}\n// Output: {\"time\":1516134303,\"level\":\"debug\",\"message\":\"hello world\"}\n```\n\u003e **Note:** By default, log writes to StdErr at the log level of `.debug`\n\u003e \n\u003e The default log level **global filter** depends on the build mode:\n\u003e - .Debug =\u003e .debug\n\u003e - .ReleaseSafe =\u003e .info\n\u003e - .ReleaseFast, .ReleaseSmall =\u003e .warn\n\n### Contextual Logging\nLoggers create events, which do the log writing.\nYou can add strongly-typed key:value pairs to an event context.\nThen the `msg`, `msgf`, or `send` method will write the event to the log.\n**Note:** Without calling any of those 3 methods, the log will not be written.\n\n```zig\nconst zlog = @import(\"zlog\");\nconst log = \u0026zlog.json_logger;\n\npub fn main() anyerror!void {\n    var ev = try log.event(.debug);\n    try ev.str(\"Scale\", \"833 cents\");\n    try ev.num(\"Interval\", 833.09);\n    try ev.msg(\"Fibonacci is everywhere\");\n\n    var ev2 = try log.event(.debug);\n    try ev2.str(\"Name\", \"Tom\");\n    try ev2.send();\n}\n// Output: {\"time\":1649450646,\"level\":\"debug\",\"Scale\":\"833 cents\",\"Interval\":833.09,\"message\":\"Fibonacci is everywhere\"}\n// Output: {\"time\":1649450646,\"level\":\"debug\",\"Name\":\"Tom\"}\n```\n\nYou can add context to a logger so that every event it creates also has that context.\nYou can also create subloggers that use the parent logger's context along with their own context.\n\n```zig\nconst zlog = @import(\"zlog\");\nconst log = \u0026zlog.json_logger;\n\npub fn main() anyerror!void {\n    try log.strCtx(\"component\", \"foo\");\n\n    var ev = try log.event(.info);\n    try ev.msg(\"hello world\");\n\n    // create sublogger, bringing along log's context \n    var sublog = try log.sublogger(.info);\n    try sublog.numCtx(\"num\", 10);\n\n    var ev2 = try sublog.event(.debug);\n    try ev2.msg(\"hey there\");\n}\n// Output: {\"time\":1649450791,\"level\":\"info\",\"component\":\"foo\",\"message\":\"hello world\"}\n// Output: {\"time\":1649450791,\"level\":\"debug\",\"component\":\"foo\",\"num\":10,\"message\":\"hey there\"}\n```\n\n### Leveled Logging\n\nzlog allow for logging at the following levels (from highest to lowest):\n- panic\n- fatal\n- error\n- warn\n- info\n- debug\n- trace\n\nA comptime-known level will be passed into `log.event(LEVEL)` or `log.sublogger(LEVEL)`\nfor leveled logging.\n\nTo disable logging entirely, set the global log level filter to `.off`;\n\n#### Setting Global Log Level Filter\n\n```zig\nconst std = @import(\"std\");\nconst zlog = @import(\"zlog\");\n\n// setting global log configuration\nconst log_conf = zlog.LogConfig{\n    .min_log_lvl = .trace, // lowest shown log level\n    .time_fmt = .unix_secs, // format to print the time\n    .buf_size = 1000, // buffer size for events, 1000 is the default\n};\n// creating a log manager with the set config\nconst log_man = zlog.LogManager(log_conf);\n// choosing a default writer to write logs into\nconst log_writer = std.io.getStdErr().writer();\n// choosing a default log level for the logger\nconst default_log_lvl = .info;\n\n// Creating the logger\nvar log = log_man.Logger(@TypeOf(log_writer), .json, default_log_lvl)\n    .new(log_writer) catch @panic(\"Failed to create global JSON logger\");\n\npub fn main() anyerror!void {\n    var ev = try log.event(.debug);\n    try ev.str(\"Scale\", \"833 cents\");\n    try ev.num(\"Interval\", 833.09);\n    try ev.msg(\"Fibonacci is everywhere\");\n\n    var ev2 = try log.event(.debug);\n    try ev2.str(\"Name\", \"Tom\");\n    try ev2.send();\n}\n// Output: {\"time\":1649450953,\"level\":\"debug\",\"Scale\":\"833 cents\",\"Interval\":833.09,\"message\":\"Fibonacci is everywhere\"}\n// Output: {\"time\":1649450953,\"level\":\"debug\",\"Name\":\"Tom\"}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandrewlee14%2Fzlog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcandrewlee14%2Fzlog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandrewlee14%2Fzlog/lists"}