问题
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?
Any help would be appreciated.
回答1:
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
- Jonathan de Boyne Pollard (2015). "Logging". The daemontools family. Frequently Given Answers.
- Jonathan de Boyne Pollard (2001). "Don't use `syslog().". Mistakes to avoid when designing Unix dæmon programs. Frequently Given Answers.
- Laurent Bercot (2011). "The Logging Chain" The s6-log program. s6.
- sd-journal. Freedesktop.org.
- https://superuser.com/a/868519/38062
- What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?
来源:https://stackoverflow.com/questions/30944427/how-do-i-create-a-log-entry-for-the-systemd-journal