Logging in systemd environment

Torsten Hilbrich torsten.hilbrich at secunet.com
Wed May 17 08:19:56 UTC 2017


Hello,

I tried to integrate ModemManager in our environment where the
modemmanager is started as systemd unit and logging is stored into the
journal.

I tried different settings, using the default syslog mechanism and using
the --log-file=/dev/stderr option (and setting up the systemd unit to
forward StandardOutput=journal) and found the log messages to contain
too much meta information which are already provided by syslog/journal
itself, including:

- program name
- pid
- log level
- timestamp

This makes the log messages quite clumsy to work with.

I would like to integrate some form to either send messages directly to
syslog in _mm_log or to directly use the journald logging with functions
like sd_journal_send. The latter could retain support for adding meta
information on the function and the code line in the log output.

I prototyped some code replacing the _mm_log and would like to know what
is the best way to integrate such optional support into the upstream
code base.

A simple idea would be to create an additional function pointer which is
used in _mm_log to multiplex the log output. One version could be used
for outputting to the logfile (where currently logfd is checked),
another version could be used for syslog and a third one for logging
to the journal.

For logging to the journal either some configure option could be added
or a command line option similar to the logfile option.

What would be your preferences?

	Torsten

Here is a version of _mm_log I used for testing the journal output.

#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
...
void
_mm_log (const char *loc,
         const char *func,
         guint32 level,
         const char *fmt,
         ...)
{
    va_list args;
    int syslog_priority = LOG_INFO;
    const char *line = 0;
    size_t file_length = 0;

    if (g_once_init_enter (&msgbuf_once)) {
        msgbuf = g_string_sized_new (512);
        g_once_init_leave (&msgbuf_once, 1);
    } else
        g_string_truncate (msgbuf, 0);

    switch (level) {
    case LOGL_ERR: syslog_priority = LOG_ERR; break;
    case LOGL_WARN: syslog_priority = LOG_WARNING; break;
    case LOGL_INFO: syslog_priority = LOG_INFO; break;
    case LOGL_DEBUG: syslog_priority = LOG_DEBUG; break;
    }

    va_start (args, fmt);
    g_string_append_vprintf (msgbuf, fmt, args);
    va_end (args);

    line = strstr(loc, ":");
    if (line != NULL) {
        file_length = line - loc;
	line++;
    } else {
	file_length = strlen(loc);
        line = "";
    }

    sd_journal_send("MESSAGE=%s", msgbuf->str,
                    "PRIORITY=%d", syslog_priority,
                    "CODE_FUNC=%s", func,
                    "CODE_FILE=%.*s", file_length, loc,
                    "CODE_LINE=%s", line,
                    NULL);
}




More information about the ModemManager-devel mailing list