Play nicely with journalctl's colourisation. #37
Labels
No labels
Bug
BuildIssue
Claude-fixed
Enhancement
In Progress
Low priority
On Hold
Rejected
Security
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
keyop-go/nfq_forwarder#37
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Currently
journalctl --user -u nfq_forwarder --followwill show logs, but we lose both the colourisation of the go logger and any journalctl colourisation.I believe journalctl can show colours based on messages (but I could be wrong about that). If it can, ensure our messages can properly trigger these colourisations as we would expect, e.g. 'errors' in red.
changed the description
Investigation + fix
Confirmed: journalctl colourises journal entries by their real
PRIORITYfield (severity), notby parsing message text. Today, every line nfq_forwarder writes to stderr under systemd gets the
same blanket priority, since a plain byte stream carries no per-line signal - so journalctl
can't tell an
Infoline from anErrorone and colourises nothing.systemd's own mechanism for exactly this is
SyslogLevelPrefix=yes(already systemd's defaultfor a journal-captured service, now made explicit in
docs/nfq_forwarder.serviceforself-documentation): journald parses a
<N>prefix (0-7, the traditional syslog/kernelprintk()priority scale) at the start of each line and uses it as that entry's real priority,stripping the prefix from the stored message.
internal/log/syslog_prefix.goadds this:syslogPrioritymaps our five levels onto that scale(
Debug→7,Info→6,Warn→4,Error→3,FatalLevel→2 "crit"), and a smallprefixWriter/syslogPrefixHandlerpair (following the exact same wrapping patternbufferHandleralready uses) prepends the right<N>to each line - only when stderr isn't aterminal (an interactive session already gets tint's own ANSI colours; the raw prefix would just
be noise there).
Verified end to end against a real journald, not just unit tests: piped a small throwaway
program's output through
systemd-cat --level-prefix=true(mirroring the unit'sSyslogLevelPrefix=yes) and confirmed viajournalctl --output=jsonthat each entry'sPRIORITYfield exactly matches (7/6/4/3 for debug/info/warn/error) with the prefix correctlystripped from the message, and via the pager's raw ANSI escapes that journalctl renders DEBUG
dim grey, INFO plain, WARN bold yellow, and ERROR bold red - exactly the behaviour requested.
make lint/make testclean.