Linux syslog 编程
2017-12-19 本文已影响88人
louyang
GNU C库提供了下述 syslog 相关API:
- openlog() 打开 syslog 连接
- syslog(), vsyslog() 向 syslog 中写内容
- closelog() 关闭 syslog 连接
- setlogmask() 可以让一些级别的消息被忽略
# cat syslog-example.c
#include <syslog.h>
int main()
{
openlog(0, 0, 0);
syslog(LOG_NOTICE, "this is a debug msg");
syslog(LOG_INFO, "this is an info msg");
syslog(LOG_ERR, "this is an error msg");
closelog();
}
# gcc syslog-example.c -o syslog-example && ./syslog-example && journalctl -f
-- Logs begin at Tue 2017-12-19 02:10:07 EET. --
...
Dec 19 08:11:18 euca-10-254-36-5.eucalyptus.internal syslog-example[1960]: this is a debug msg
Dec 19 08:11:18 euca-10-254-36-5.eucalyptus.internal syslog-example[1960]: this is an info msg
Dec 19 08:11:18 euca-10-254-36-5.eucalyptus.internal syslog-example[1960]: this is an error msg
参考
https://www.gnu.org/software/libc/manual/html_node/Submitting-Syslog-Messages.html