lg2: use DEBUG_INVOCATION to reduce debug journal
The systemd-v257 release introduced a new convention that can be used
by daemons to reduce the amount of calls to journald for debug-level
messages: DEBUG_INVOCATION[1]. Daemons will typically start with
DEBUG_INVOCATION not set, but in the event of a crash, the daemon will
be restarted with it set. Daemons are expected to emit higher debug
messages when ran in that mode.
Adjusting the logger code to avoid sending debug messages to journald
unless DEBUG_INVOCATION is set. This reduces the overhead of logging
by skipping the journald calls entirely.
[1]: https://github.com/systemd/systemd/commit/7d8bbfbe0852ec89590d1dc5e28afc95d6d44fa1
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I5896c4714b92786c8f859f2e5ab389753e4b5c26
diff --git a/lib/lg2_logger.cpp b/lib/lg2_logger.cpp
index 57fc029..3d28daf 100644
--- a/lib/lg2_logger.cpp
+++ b/lib/lg2_logger.cpp
@@ -214,6 +214,10 @@
? cerr_extra_output
: noop_extra_output;
+// Skip sending debug messages to journald if "DEBUG_INVOCATION" is not set
+// per systemd.exec manpage.
+static auto send_debug_to_journal = nullptr != getenv("DEBUG_INVOCATION");
+
// Do_log implementation.
void do_log(level l, const std::source_location& s, const char* m, ...)
{
@@ -300,7 +304,10 @@
});
// Output the iovec.
- sd_journal_sendv(iov.data(), strings.size());
+ if (send_debug_to_journal || l != level::debug)
+ {
+ sd_journal_sendv(iov.data(), strings.size());
+ }
extra_output_method(l, s, message);
}