{"id":28027093,"url":"https://github.com/reykjalin/log_to_file","last_synced_at":"2025-10-03T22:35:02.306Z","repository":{"id":289416261,"uuid":"896209788","full_name":"reykjalin/log_to_file","owner":"reykjalin","description":"Have Zig's `std.log` log to a file instead of to stderr","archived":false,"fork":false,"pushed_at":"2025-04-24T02:38:21.000Z","size":14,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-24T03:29:07.927Z","etag":null,"topics":["logging","zig","zig-package"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reykjalin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-29T19:29:24.000Z","updated_at":"2025-04-24T02:53:26.000Z","dependencies_parsed_at":"2025-04-24T03:29:09.307Z","dependency_job_id":null,"html_url":"https://github.com/reykjalin/log_to_file","commit_stats":null,"previous_names":["reykjalin/log_to_file.zig","reykjalin/log_to_file"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reykjalin%2Flog_to_file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reykjalin%2Flog_to_file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reykjalin%2Flog_to_file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reykjalin%2Flog_to_file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reykjalin","download_url":"https://codeload.github.com/reykjalin/log_to_file/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253528540,"owners_count":21922623,"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","zig-package"],"created_at":"2025-05-11T06:34:47.793Z","updated_at":"2025-10-03T22:34:55.268Z","avatar_url":"https://github.com/reykjalin.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# log_to_file\n\n[API docs](https://reykjalin.srht.site/docs/log_to_file/)\n\nAn easy way to change Zig's default `std.log` functions to write to a file instead of logging to\nstderr.\n\n\u003e [!NOTE]\n\u003e **This is not a logging library!**\n\u003e It offers a function you can pass to Zig's `std_options`'s `.logFn` so `std.log` calls write to a\n\u003e file instead of stderr.\n\n\u003e [!IMPORTANT]\n\u003e **Version 2.0.0 introduced breaking changes**. Read through this readme to see what changed, or\n\u003e check [the changelog](./CHANGELOG.md).\n\n## Usage\n\n\u003e [!IMPORTANT]\n\u003e If the logging function fails to write to the log file, e.g. due to a permissions issue, it will\n\u003e fallback to `std.log.defaultLog()` instead of silently failing.\n\n1. Install the library by running\n   `zig fetch --save https://git.sr.ht/~reykjalin/log_to_file/archive/2.1.0.tar.gz`\n   in your project.\n    * You can also use `zig fetch --save https://github.com/reykjalin/log_to_file/archive/refs/tags/2.1.0.zip` if you prefer.\n2. Add the library as a dependency in your project's `build.zig`:\n\n```zig\npub fn build(b: *std.Build) void {\n    const target = b.standardTargetOptions(.{});\n    const optimize = b.standardOptimizeOption(.{});\n\n    const ltf_dep = b.dependency(\"log_to_file\", .{ .target = target, .optimize = optimize });\n\n    const exe = b.addExecutable(.{\n        .name = \"app\",\n        .root_source_file = b.path(\"src/main.zig\"),\n        .target = target,\n        .optimize = optimize,\n    });\n\n    exe.root_module.addImport(\"log_to_file\", ltf_dep.module(\"log_to_file\"));\n```\n\n3. Set the log function in your main project:\n\n```zig\nconst ltf = @import(\"log_to_file\");\n\npub const std_options: std.Options = .{\n    .logFn = ltf.log_to_file,\n};\n```\n\nNow, whenever you call the `std.log` functions they should be written to a\n`logs/\u003cexecutable_name\u003e.log` file in your current working directory when you make a `Debug` build,\nand `~/.local/state/logs/\u003cexecutable_name\u003e.log` in a `Release` build.\n\nFor example, if your executable is called `example` (as is the case with\n[the examples](./examples/README.md)) the logs will be in `./logs/example.log` by default in a\n`Debug` build.\n\n## Configuration\n\n[The examples](./examples/README.md) are a great resource to play with to understand how the\nlibrary works.\n\nIf you'd like to write logs to a different path you can configure that by adding this to your root\nsource file (typically `src/main.zig` or similar):\n\n```zig\nconst ltf = @import(\"log_to_file\");\n\n// Logs will be saved to:\n//   * ./logs/log in Debug mode.\n//   * ~/.local/state/logs/log in Release mode.\npub const log_to_file_options: ltf.Options = .{\n    .log_file_name = \"log\",\n};\n\n// Logs will be saved to:\n//   * ./new-logs/\u003cexecutable_name\u003e.log in Debug mode.\n//   * ./new-logs/\u003cexecutable_name\u003e.log in Release mode.\npub const log_to_file_options: ltf.Options = .{\n    .storage_path = \"new-logs\",\n};\n\n// Logs will be saved to:\n//   * /var/logs/\u003cexecutable_name\u003e.log in Debug mode.\n//   * /var/logs/\u003cexecutable_name\u003e.log in Release mode.\npub const log_to_file_options: ltf.Options = .{\n    .storage_path = \"/var/logs\",\n};\n\n// Logs will be saved to:\n//   * ./app-logs/app.log in Debug mode.\n//   * ./app-logs/app.log in Release mode.\npub const log_to_file_options: ltf.Options = .{\n    .log_file_name = \"app.log\",\n    .storage_path = \"app-logs\",\n};\n\n// Logs will be saved to:\n//   * /var/logs/app.log in Debug mode.\n//   * /var/logs/app.log in Release mode.\npub const log_to_file_options: ltf.Options = .{\n    .log_file_name = \"app.log\",\n    .storage_path = \"/var/logs\",\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freykjalin%2Flog_to_file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freykjalin%2Flog_to_file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freykjalin%2Flog_to_file/lists"}