ReviewOS

also looking at this

craft-native/craft

fix(log): keep the terminal's colour, and let logging be turned off

#81
Merged glennmichael123 wants to merge fix/log-colour-and-off into main
4 files +126 -16
10 days ago

The two loose ends I flagged after #78.

Colour

Both sinks were handed the same bytes, so --log-file had to choose: ANSI escapes in the file, or no colour on the terminal. I took the second in #78 — which is a strange trade, since asking for a log file shouldn't change what the terminal looks like.

The plain line is formatted always; a coloured one is formatted separately, for stderr, and only when stderr is a terminal — colour written into a redirected stderr is the same problem as colour written into the file. Decided once at init rather than per record, since it can't change for a running process.

beforeafter
file contentsclean only because colour was disabled globallyalways clean
terminal with --log-filecolour lostcolour kept
redirected stderrcolourclean

LogLevel.Off

logging.zig has had an off since it was written; this side had no way to say the same thing, so an app that wanted quiet had to not configure logging at all — which is not the same as asking for it.

It goes last in the enum so the existing level >= min_level comparison does the silencing and no other branch has to learn the concept. There's a test pinning that ordering, because it's the reason the rest of the file needed no changes. --log-level accepts off, none and silent.

The colour test was worthless when I first wrote it

A test process has a piped stderr, so stderr_is_tty was false, so no coloured line was ever produced and the separation the test claimed to check was never exercised. It passed against a deliberately reverted fix.

It forces the terminal condition now, and the control fires: sending the coloured bytes to both sinks again fails it.

That's the third time in this stretch that writing the control is what exposed a test asserting nothing. I'd rather write the control first from here on.

Verified

Empirically, by running the binary:

default            → 1 record in the file, 0 ANSI bytes
--log-level off    → file created, 0 records
--log-level warn   → 1 record, 0 ANSI bytes
--log-level bogus  → "expected debug, info, warn, error, fatal or off"

Plus zig build · zig build test · zig build test-js · x86_64-windows cross-build · zig fmt --check · 511 bun tests · pickier 38 warnings, unchanged from main.

4 changed files on the files tab, with 0 review threads.

Sign in to comment on this pull request.