diff --git a/README.md b/README.md index d9b0a8d..ffc7fa3 100644 --- a/README.md +++ b/README.md @@ -89,3 +89,19 @@ This will result in the following output: For more details about the output customization see the configuration options of the `zlog` module. Following options are available: - _timestamp_ (default: `true`): Prepend the current timestamp before each log message. + +## Tips + +The following list shows some tips on how to use logging more effectively. These tips do not apply just to `zlog` (and not even only to zig code). + +- Use `errdefer` to directly print messages on failures in the same function they occur: + ```zig + // assume log is already defined before (with the corresponding scope) + const port = port: { + errdefer |err| log.err("failed to read the port number: {}", .{err}); + + var buf: [fmt.count("{}\n", .{maxInt(u16)})]u8 = undefined; + const len = try process.stdout.?.readAll(&buf); + break :port try fmt.parseInt(u16, buf[0 .. len -| 1], 10); + }; + ```