You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: readme.md
+18-4
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
Structured Logging for Zig
2
2
3
-
logz is an opinionated structured logger that outputs to stdout using the logfmt format. It aims to minimize runtime memory allocation by using a pool of pre-allocated loggers.
3
+
logz is an opinionated structured logger that outputs to stdout using logfmt or JSON. It aims to minimize runtime memory allocation by using a pool of pre-allocated loggers.
4
4
5
5
# Installation
6
6
This library supports native Zig module (introduced in 0.11). Add a "logz" dependency to your `build.zig.zon`.
@@ -135,11 +135,13 @@ pub const Config = struct {
135
135
136
136
// Where to write the output: can be either .stdout or .stderr
137
137
output: Output = .stdout,
138
+
139
+
encoding: Encoding = .kv, // or .json
138
140
};
139
141
```
140
142
141
143
### Timestamp and Level
142
-
When using the `debug`, `info`, `warn`, `err` or `fatal` functions, logs will always begin with `@ts=$MILLISECONDS_SINCE_JAN1_1970_UTC @l=$LEVEL`, such as: `@ts=1679473882025 @l=INFO`.
144
+
When using the `debug`, `info`, `warn`, `err` or `fatal` functions, logs will always begin with `@ts=$MILLISECONDS_SINCE_JAN1_1970_UTC @l=$LEVEL`, such as: `@ts=1679473882025 @l=INFO`. With JSON encoding, the object will always have the `"@ts"` and `"@l"` fields.
143
145
144
146
### Logger Life cycle
145
147
The logger is implicitly returned to the pool when `log`, `logTo` or `tryLog` is called. In rare cases where `log`, `logTo` or `tryLog` are not called, the logger must be explicitly released using its `release()` function:
@@ -148,6 +150,8 @@ The logger is implicitly returned to the pool when `log`, `logTo` or `tryLog` is
148
150
// This is a contrived example to show explicit release
149
151
var l = logz.info();
150
152
_ = l.string("key", "value");
153
+
154
+
// actually, on second thought, I don't want to log anything
151
155
l.release();
152
156
```
153
157
@@ -205,7 +209,7 @@ pub fn main() !void {
205
209
### Prefixes
206
210
A pool can be configured with a prefix by setting the `prefix` field of the configuration. When set, all log entries generated by loggers of this pool will contain the prefix.
207
211
208
-
The prefix is written as-is, with no escape and no enforcement of being a key=value.
212
+
The prefix is written as-is.
209
213
210
214
```zig
211
215
// prefix can be anything []const u8. It doesn't have to be a key=value
The above will generate a log line: `keemun @ts=TIMESTAMP @l=INFO tea=Y"`
220
224
225
+
When using `.json` encoding, your prefix must begin the object:
226
+
227
+
```zig:
228
+
var p = try logz.Pool.init(allocator, .{.prefix = "=={"});
229
+
defer p.deinit();
230
+
231
+
p.info().boolean("tea", true).log();
232
+
```
233
+
The above will generate a log line: `=={"@ts":TIMESTAMP, "@l":"INFO", "tea":true}`
234
+
221
235
### Multi-Use Logger
222
-
Rather than having a logger automatically returned to the pool when `.log()` or `tryLog()` are called, it is possible to flag the logger for "multi-use". In such cases, the logger must be explicitly returned to the pool using `logger.release()`. This can be enabled by calling `multiuse` on the logger. Important, and the reason this feature exists, is logs by the same logger will share the same attributes up to the point where multiuse was called:
236
+
Rather than having a logger automatically returned to the pool when `.log()` or `tryLog()` are called, it is possible to flag the logger for "multi-use". In such cases, the logger must be explicitly returned to the pool using `logger.release()`. This can be enabled by calling `multiuse` on the logger. Logs created by the logger will share the same attributes up to the point where multiuse was called:
223
237
224
238
```zig
225
239
var logger = logz.logger().string("request_id", request_id).multiuse();
0 commit comments