Search code examples
servicesystemd

How do I create a log entry for the systemd journal?


I have a service, and I would like to have it create logs for the journald daemon in certain cases. So far, I have not been able to find any instructions on how to do so.

Am I misunderstanding the intended use of the journal? Or is it something really obvious that I am missing?


Solution

  • If you have a service, you write your log to standard error. (It's even available as a stream named std::clog in C++, with more "log-like" semantics than std::cerr.) That's it. It's a logging mechanism that works with systemd, daemontools, daemontools-encore, runit, s6, perp, nosh, freedt, and others.

    There is an API for writing to the systemd journal. Make sure that you have an excellent reason for locking your softwares and your users in to that API, however. Writing to standard error is a mechanism that just works, pretty much everywhere. It's well understood and easy for administrators to control, tweak, and comprehend. It should be the first choice.

    Further reading