Skip to content

Add systemd_namespace_sink for logging to specific systemd namespace#3606

Merged
gabime merged 5 commits into
gabime:v1.xfrom
oko256:systemd_namespace_sink
May 11, 2026
Merged

Add systemd_namespace_sink for logging to specific systemd namespace#3606
gabime merged 5 commits into
gabime:v1.xfrom
oko256:systemd_namespace_sink

Conversation

@oko256
Copy link
Copy Markdown
Contributor

@oko256 oko256 commented May 5, 2026

Adds systemd_namespace_sink that can open a stream to a specific systemd journal namespace for logging. This was created as a separate sink from systemd_sink because it has significant difference due to systemd's journal API: sd_journal_send() does not have namespace-specific counterpart and thus this cannot utilize other extra journal fields than PRIORITY (via kernel-style log level prefixes).

Another difference is that this namespace stream API is only available from systemd version 256, thus a version check was added to CMake file for the unit tests so that they are not automatically included if libsystemd is not new enough.

Unit tests are split to two parts: first one runs always and it just uses the stream to log to default namespace (should always work). Second one requires manual setting up of "custom" journal namespace and thus is ignored from the default set of tests (can be run by specifying [systemd_namespace] tag explicitly when running tests). On the other hand, the second part also automatically checks the output correctness from journal.

Adds systemd_namespace_sink that can open a stream to a specific systemd
journal namespace for logging. This was created as a separate sink from
systemd_sink because it has significant difference due to systemd's
journal API: `sd_journal_send()` does not have namespace-specific
counterpart and thus this cannot utilize other extra journal fields than
`PRIORITY` (via kernel-style log level prefixes).

Another difference is that this API is only available from systemd
version 256, thus a version check was added to CMake file for the unit
tests so that they are not automatically included if `libsystemd` is not
new enough.

Unit tests are split to two parts: first one runs always and it just
uses the stream to log to default namespace (should always work). Second
one requires manual setting up of "custom" journal namespace and thus is
ignored from the default set of tests (can be run by specifying
`[systemd_namespace]` tag explicitly when running tests). On the other
hand, the second part also automatically checks the output correctness
from journal.
@oko256 oko256 marked this pull request as ready for review May 5, 2026 18:41
@gabime gabime requested a review from Copilot May 6, 2026 16:20
Comment thread include/spdlog/sinks/systemd_namespace_sink.h
Comment thread include/spdlog/sinks/systemd_namespace_sink.h Outdated
Comment thread include/spdlog/sinks/systemd_namespace_sink.h Outdated
`SD_JOURNAL_SUPPRESS_LOCATION` is not needed by this sink since it does
not use the APIs that add the location info.

Moved level prefixes to `static constexpr` to indicate those being
compile-time constants (and also saves a bit space).

Removed message length truncation since with this stream API we don't
even know the maximum length of the messages. Journald truncates the
message at its end based on its own configuration.

Added error checking to `fwrite_bytes()` calls similarly to other sinks.

Optimized away excess `\n` check.

Added `flush_()` implementation.
@oko256
Copy link
Copy Markdown
Contributor Author

oko256 commented May 7, 2026

Did a bunch of fixes, cleanup, and some optimizations. See the commit message for details.

Comment thread include/spdlog/sinks/systemd_namespace_sink.h Outdated
Comment thread include/spdlog/sinks/systemd_namespace_sink.h
Comment thread include/spdlog/sinks/systemd_namespace_sink.h Outdated
Comment thread include/spdlog/sinks/systemd_namespace_sink.h Outdated
Comment thread include/spdlog/sinks/systemd_namespace_sink.h
@gabime gabime merged commit 2e71fdf into gabime:v1.x May 11, 2026
13 of 14 checks passed
@gabime
Copy link
Copy Markdown
Owner

gabime commented May 11, 2026

Thanks @oko256 !

@oko256 oko256 deleted the systemd_namespace_sink branch May 11, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants