···11+# Changelog
22+33+## [work-in-progress] 2.0.0
44+55+### Bug fixes
66+77+* Removed general purpose allocator initialization that was unused. This was optimized away by the
88+ Zig compiler, but the code shouldn't be there.
99+1010+### Breaking changes
1111+1212+* `pub const log_to_file_path` no longer works. Use `pub const log_to_file_options` instead. See
1313+ [the readme](./README.md) for deatils or take a look at [the examples](./examples/README.md).
1414+* The default, no-config log location is now `./logs/out.log` **in `Debug` mode**.
1515+* The default, no-config log location is now `~/.local/logs/out.log` **in `ReleaseFast`,
1616+ `ReleaseSmall`, and `ReleaseSafe` modes**.
1717+1818+1919+## 1.0.0
2020+2121+* Logs to `./log` by default.
2222+* You can change log file path by setting `pub const log_to_file_path = "/new/path/to/log"` in
2323+ you root source file (typically `src/main.zig` or similar).
+45-14
README.md
···11# log_to_file.zig
2233-An easy way to change the default `std.log` functions to write to a file instead of logging to
44-STDOUT.
55-Simply add this to your project via
66-77-and `std.log` functions will write to a file called `log` in your current working directory.
33+An easy way to change Zig's default `std.log` functions to write to a file instead of logging to
44+STDOUT/STDERR.
8599-> **NOTE**
1010->
66+> [!NOTE]
117> **This is not a logging library!**
128> It offers a function you can pass to Zig's `std_options`'s `.logFn` so `std.log` calls write to a
139> file instead of STDOUT.
14101111+> [!IMPORTANT]
1212+> **Version 2.0.0 introduced breaking changes**. Read through this readme to see what changed, or
1313+> check [the changelog](./CHANGELOG.md).
1414+1515## Usage
161617171. Install the library by running
1818- `zig fetch --save https://git.sr.ht/~reykjalin/log_to_file.zig/archive/1.0.0.tar.gz`
1818+ `zig fetch --save git+https://git.sr.ht/~reykjalin/log_to_file.zig`
1919 in your project.
2020+ * You can also use `zig fetch --save git+https://github.com/reykjalin/log_to_file.zig.git` if you prefer.
20212. Add the library as a dependency in your project's `build.zig`:
21222223```zig
···4647};
4748```
48494949-Now, whenever you call the `std.log` functions they should be written to a `log` file in your
5050-current working directory.
5050+Now, whenever you call the `std.log` functions they should be written to a `logs/out.log` file in
5151+your current working directory when you make a `Debug` build, and `~/.local/logs/out.log` in a
5252+`Relase` build.
51535254## Configuration
53555656+[The examples](./examples/README.md) are a great resource to play with to understand how the
5757+library works.
5858+5459If you'd like to write logs to a different path you can configure that by adding this to your root
5560source file (typically `src/main.zig` or similar):
56615762```zig
5858-// Relative to current working directory:
5959-pub const log_to_file_path = "logs/app.log";
6363+const ltf = @import("log_to_file");
6464+6565+// Logs will be saved to:
6666+// * ./logs/log in Debug mode.
6767+// * ~/.local/logs/log in Release mode.
6868+pub const log_to_file_options: ltf.Options = .{
6969+ .log_file_name = "log",
7070+};
7171+7272+// Logs will be saved to:
7373+// * ./new-logs/out.log in Debug mode.
7474+// * ./new-logs/out.log in Release mode.
7575+pub const log_to_file_options: ltf.Options = .{
7676+ .storage_path = "new-logs",
7777+};
7878+7979+// Logs will be saved to:
8080+// * ./app-logs/out.log in Debug mode.
8181+// * ./app-logs/out.log in Release mode.
8282+pub const log_to_file_options: ltf.Options = .{
8383+ .log_file_name = "app.log",
8484+ .storage_path = "app-logs",
8585+};
60866161-// Absolute path:
6262-pub const log_to_file_path = "/var/logs/app.log";
8787+// Logs will be saved to:
8888+// * /var/logs/app.log in Debug mode.
8989+// * /var/logs/app.log in Release mode.
9090+pub const log_to_file_options: ltf.Options = .{
9191+ .log_file_name = "app.log",
9292+ .storage_path = "/var/logs",
9393+};
6394```
···11-// Copyright 2024 - 2024, Kristófer Reykjalín and the log_to_file contributors.
11+// Copyright 2024 - 2025, Kristófer Reykjalín and the log_to_file contributors.
22// SPDX-License-Identifier: BSD-3-Clause
3344.{
55- // This is the default name used by packages depending on this one. For
66- // example, when a user runs `zig fetch --save <url>`, this field is used
77- // as the key in the `dependencies` table. Although the user can choose a
88- // different name, most users will stick with this provided value.
99- //
1010- // It is redundant to include "zig" in this name because it is already
1111- // within the Zig package namespace.
1212- .name = "log_to_file",
1313-1414- // This is a [Semantic Version](https://semver.org/).
1515- // In a future version of Zig it will be used for package deduplication.
1616- .version = "1.0.0",
1717-1818- // This field is optional.
1919- // This is currently advisory only; Zig does not yet do anything
2020- // with this value.
2121- .minimum_zig_version = "0.13.0",
2222-2323- // Specifies the set of files and directories that are included in this package.
2424- // Only files and directories listed here are included in the `hash` that
2525- // is computed for this package. Only files listed here will remain on disk
2626- // when using the zig package manager. As a rule of thumb, one should list
2727- // files required for compilation plus any license(s).
2828- // Paths are relative to the build root. Use the empty string (`""`) to refer to
2929- // the build root itself.
3030- // A directory listed here means that all files within, recursively, are included.
55+ .name = .log_to_file,
66+ .fingerprint = 0x53bc1f48033cc1bd,
77+ .version = "2.0.0",
88+ .minimum_zig_version = "0.14.0",
319 .paths = .{
3210 "build.zig",
3311 "build.zig.zon",
3412 "src",
1313+ "CHANGELOG.md",
3514 "LICENSE",
3615 "README.md",
3716 },
+19
examples/README.md
···11+# Examples
22+33+You can run the examples by running `zig build -Dexample=<example> run` where `<example>` is one
44+of:
55+66+1. `defaults` - demonstrates no-config behavior.
77+2. `custom_log_file` - demonstrates custom log file name behavior.
88+3. `custom_storage_path` - demonstrates custom storage path behavior.
99+4. `custom_storage_path_and_log_file` - demonstrates custom storage path and log file name
1010+behavior.
1111+1212+## Running examples in release mode
1313+1414+The command above will run the examples in `Debug` mode. You can also run them in `Release` mode
1515+with `zig build -Doptimize=<example> -Doptimize=ReleaseFast run`.
1616+1717+> [!IMPORTANT]
1818+> When in release mode the `defaults` and `custom_log_file` examples will store logs in
1919+> `~/.local/logs/out.log` and `~/.local/logs/custom-file.log` respectively.