log: Add $SYSTEMD_LOG_EXECUTABLE to prefix logs with executable name

We have various cases where we do callouts to our own binaries and
when debugging it can be very useful to know which binary is logging
some message so add an environment variable to allow doing just that.
This commit is contained in:
Daan De Meyer authored and Daan De Meyer committed 2026-10-05 19:36:16 +02:00
1 parent 88c3ce52cc
commit 61eb00e672
4 files changed
+45 -1

No files matched your search

+11
View File
@@ -94,6 +94,17 @@
</para></listitem>
</varlistentry>
<varlistentry id='log-executable'>
<term><varname>$SYSTEMD_LOG_EXECUTABLE</varname></term>
<listitem><para id='log-executable-body'>A boolean. If true, messages will be prefixed with the current
executable name.</para>
<para>Note that this information is attached as metadata to journal entries anyway. Including it
directly in the message text can nevertheless be convenient when debugging programs.</para>
</listitem>
</varlistentry>
<varlistentry id='pager'>
<term><varname>$SYSTEMD_PAGER</varname></term>
<term><varname>$PAGER</varname></term>
+1
View File
@@ -172,6 +172,7 @@
<term><varname>$SYSTEMD_LOG_TIME</varname></term>
<term><varname>$SYSTEMD_LOG_COLOR</varname></term>
<term><varname>$SYSTEMD_LOG_LOCATION</varname></term>
<term><varname>$SYSTEMD_LOG_EXECUTABLE</varname></term>
<listitem><para>Same as in
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.</para>
+30 -1
View File
@@ -65,6 +65,7 @@ static int show_color = -1; /* tristate */
static bool show_location = false;
static bool show_time = false;
static bool show_tid = false;
static bool show_executable = false;
static bool upgrade_syslog_to_journal = false;
static bool always_reopen_console = false;
@@ -423,7 +424,7 @@ static int write_to_console(
header_time[FORMAT_TIMESTAMP_MAX],
prefix[1 + DECIMAL_STR_MAX(int) + 2],
tid_string[3 + DECIMAL_STR_MAX(pid_t) + 1];
struct iovec iovec[11];
struct iovec iovec[13];
const char *on = NULL, *off = NULL;
size_t n = 0;
@@ -447,6 +448,11 @@ static int write_to_console(
iovec[n++] = IOVEC_MAKE_STRING(" ");
}
if (show_executable) {
iovec[n++] = IOVEC_MAKE_STRING(program_invocation_short_name);
iovec[n++] = IOVEC_MAKE_STRING(": ");
}
if (show_tid) {
xsprintf(tid_string, "(" PID_FMT ") ", gettid());
iovec[n++] = IOVEC_MAKE_STRING(tid_string);
@@ -1384,6 +1390,10 @@ void log_parse_environment_variables(void) {
e = getenv("SYSTEMD_LOG_RATELIMIT_KMSG");
if (e && log_set_ratelimit_kmsg_from_string(e) < 0)
log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_EXECUTABLE");
if (e && log_show_executable_from_string(e) < 0)
log_warning("Failed to parse log executable '%s', ignoring.", e);
}
void log_parse_environment(void) {
@@ -1459,6 +1469,14 @@ bool log_get_show_tid(void) {
return show_tid;
}
void log_show_executable(bool b) {
show_executable = b;
}
bool log_get_show_executable(void) {
return show_executable;
}
int log_show_color_from_string(const char *e) {
int r;
@@ -1503,6 +1521,17 @@ int log_show_tid_from_string(const char *e) {
return 0;
}
int log_show_executable_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
log_show_executable(r);
return 0;
}
bool log_on_console(void) {
if (IN_SET(log_target, LOG_TARGET_CONSOLE,
LOG_TARGET_CONSOLE_PREFIXED))
+3
View File
@@ -63,11 +63,14 @@ void log_show_time(bool b);
bool log_get_show_time(void) _pure_;
void log_show_tid(bool b);
bool log_get_show_tid(void) _pure_;
void log_show_executable(bool b);
bool log_get_show_executable(void) _pure_;
int log_show_color_from_string(const char *e);
int log_show_location_from_string(const char *e);
int log_show_time_from_string(const char *e);
int log_show_tid_from_string(const char *e);
int log_show_executable_from_string(const char *e);
/* Functions below that open and close logs or configure logging based on the
* environment should not be called from library code — this is always a job