Prefix log lines with syslog priority for journalctl colourisation #85

Merged
danny merged 1 commit from issue37_journalctl_colour into main 2026-07-09 21:46:51 +01:00
Collaborator

Closes #37.

See the issue comment for the full investigation - summary: every line written to stderr under systemd got the same blanket journal priority (no per-line signal on a plain byte stream), so journalctl could not distinguish an Info line from an Error one and colourised nothing.

internal/log/syslog_prefix.go prefixes each line with <N> (the syslog/kernel priority scale systemd's SyslogLevelPrefix option parses - already systemd's own default for a journal-captured service, now made explicit in docs/nfq_forwarder.service), mapping Debug->7, Info->6, Warn->4, Error->3, FatalLevel->2 (crit). Only applied when stderr is not a terminal - an interactive session keeps tint's own ANSI colours untouched.

Verified end to end against a real journald (not just unit tests): piped a throwaway program's output through systemd-cat --level-prefix=true and confirmed via journalctl --output=json that each entry's PRIORITY field matches (7/6/4/3) with the prefix stripped from the message, and via the pager's raw ANSI escapes that journalctl renders ERROR in bold red, WARN in bold yellow, DEBUG dim grey, INFO plain.

make lint/make test clean.

Closes #37. See the issue comment for the full investigation - summary: every line written to stderr under systemd got the same blanket journal priority (no per-line signal on a plain byte stream), so journalctl could not distinguish an Info line from an Error one and colourised nothing. `internal/log/syslog_prefix.go` prefixes each line with `<N>` (the syslog/kernel priority scale systemd's `SyslogLevelPrefix` option parses - already systemd's own default for a journal-captured service, now made explicit in `docs/nfq_forwarder.service`), mapping `Debug`->7, `Info`->6, `Warn`->4, `Error`->3, `FatalLevel`->2 (crit). Only applied when stderr is not a terminal - an interactive session keeps tint's own ANSI colours untouched. Verified end to end against a real journald (not just unit tests): piped a throwaway program's output through `systemd-cat --level-prefix=true` and confirmed via `journalctl --output=json` that each entry's `PRIORITY` field matches (7/6/4/3) with the prefix stripped from the message, and via the pager's raw ANSI escapes that journalctl renders ERROR in bold red, WARN in bold yellow, DEBUG dim grey, INFO plain. `make lint`/`make test` clean.
Prefix log lines with syslog priority for journalctl colourisation
All checks were successful
CI / lint (push) Successful in 27s
CI / build (push) Successful in 27s
CI / lint (pull_request) Successful in 26s
CI / build (pull_request) Successful in 27s
CI / release (push) Has been skipped
CI / release (pull_request) Has been skipped
e690872e2b
Every line nfq_forwarder writes to stderr under systemd got the same
blanket journal priority, since a plain byte stream carries no
per-line signal - so journalctl couldn't tell an Info line from an
Error one and colourised nothing.

Prefix each line with "<N>", the syslog/kernel priority scale
systemd's SyslogLevelPrefix option (its own default for a
journal-captured service) parses at the start of a line. Only
applied when stderr isn't a terminal - an interactive session already
gets tint's own ANSI colours.

Closes #37

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
claude requested review from danny 2026-07-09 20:55:19 +01:00
danny scheduled this pull request to auto merge when all checks succeed 2026-07-09 21:41:27 +01:00
danny approved these changes 2026-07-09 21:46:48 +01:00
danny left a comment

Tested and this works as intended. Have also raised issue #86 as it wasn't clear from journalctl output when the version under test started, but that's not part of this MR.

Works well - approving.

Tested and this works as intended. Have also raised issue #86 as it wasn't clear from journalctl output when the version under test started, but that's not part of this MR. Works well - approving.
danny merged commit 8b3c4394e6 into main 2026-07-09 21:46:51 +01:00
danny deleted branch issue37_journalctl_colour 2026-07-09 21:46:51 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
keyop-go/nfq_forwarder!85
No description provided.