Repository navigation
Add opt-in syslog priority prefix for stream logs - #2703
Open
tmilnthorp wants to merge 1 commit into
Open
tmilnthorp wants to merge 1 commit into
tmilnthorp wants to merge 1 commit into
Conversation
A log written to STDOUT or STDERR with `level_prefix: syslog` starts each line with its syslog priority (<2> CRITICAL ... <7> DEBUG), so systemd-journald's SyslogLevelPrefix and similar collectors record the real level instead of treating everything as info. Every line of a multi-line record is prefixed. File logs refuse the option, and log subscriptions still receive the unprefixed line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When AppDaemon runs as a systemd service writing to stdout/stderr, journald records every line at the unit's default priority (info). Errors and warnings can't be filtered with
journalctl -p, and anything downstream of the journal can't tell them apart either. journald already understands the sd-daemon<N>prefix (SyslogLevelPrefix=, on by default), so AppDaemon just needs to emit it.Mapping: CRITICAL→2, ERROR→3, WARNING→4, INFO→6, DEBUG/NOTSET→7. A custom level uses the nearest standard level below it, so 25 maps to 6 and 45 maps to 3. Anything above CRITICAL maps to 2. Python has no notice level, so 5 is never used.
Details:
filenameisSTDOUTorSTDERR. A file log withlevel_prefixfails at startup with a clear error. The prefix is a stream protocol, and putting<3>into files that people read or rotate seemed more surprising than useful. Validation happens before the handler is created, so the file isn't opened.SyslogLevelPrefixFormatter) set only on the stream handler. The log's own formatter inLogging.configis unchanged, so log callbacks and the admin UI still get the unprefixed line.How it composes with the JSON output PR: the wrapper prefixes whatever the log's formatter produces, so with both
output_format: jsonandlevel_prefix: syslogeach line becomes<3>{"timestamp": ..., "level": "ERROR", ...}. journald strips the prefix and stores the JSON as MESSAGE. The two branches are independent and both based ondev. I trial-merged them together with the STDERR fix: the only conflict is the adjacent Features bullet in HISTORY.md, and the combined test suite passes, including that JSON+prefix case.Tests are in
tests/unit/test_logging_level_prefix.py: the level mapping (including custom levels and NOTSET), prefixes on STDOUT and STDERR logs, every traceback line prefixed, no prefix by default, log subscriptions unprefixed, file logs refused without creating the file, and invalid values rejected by bothLoggingand the config model. The tests use the sharedmake_loggingfixture intests/unit/conftest.py, identical to the one in the other two PRs.Docs: CONFIGURE.rst (logs directives) and HISTORY.md.
Part of #2700.