linuxloggingsystemdsystemd-journald

sd_journal_print is not logging properly


#include<systemd/sd-journal.h>

int main()
{
        sd_journal_print(LOG_WARNING, "Test message: Warning...");
        sd_journal_send("SD_JOURNAL_TEST=SEND Test message: Warning...", "PRIORITY=%i", LOG_WARNING, NULL);
}

Compiled with: g++ sd-journal-test.cc -o sd-journal-test -lsystemd

$journalctl --since "0.2 hours ago"

shows :

Jun 30 18:45:01 preetam-Precision-M4800 sd-journal-test[23739]: Test message: Warning...

sd_journal_print gave a log without line and other numbers and sd_journal_Send never logged.

Whats is the cause and how do I fix this?


Solution

  • You are passing invalid formatted arguments. sd_journal_send expects:

    The strings passed should be of the format "VARIABLE=value"

    Correct use:

        sd_journal_send("MESSAGE=Test message: Warning...", "SD_JOURNAL_TEST=SEND", "PRIORITY=%i", LOG_WARNING, NULL);