如何为 systemd 日志创建日志条目?

如何为 systemd 日志创建日志条目?

问题描述:

我有一个 service,我希望它在某些情况下为 journald 守护进程创建日志.到目前为止,我还没有找到任何有关如何操作的说明.

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.

我是否误解了 journal 的预期用途?还是我遗漏了一些很明显的东西?

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

如果您有一项服务,则将日志写入标准错误.(它甚至可以用作 C++ 中名为 std::clog 的流,比 std::cerr 具有更多的类日志"语义.)就是这样.这是一种日志机制,适用于 systemd、daemontools、daemontools-encore、runit、s6、perp、nosh、freedt 等.

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.

有一个用于写入 systemd 日志的 API.但是,请确保您有充分的理由将您的软件和用户锁定在该 API 中.写入标准错误是一种有效的机制,几乎无处不在.它很好理解并且易于管理员控制、调整和理解.应该是首选.

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.

  • 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?