Linux 如何配置 syslog 以便应用程序日志转到特定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10376020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure syslog so that an applications log goes to a specific file
提问by suresh
I have an application myapp
which should send log files onlyto /var/log/myapp.log
. myapp
is written in C++. The following sample code, sends the logs to /var/log/syslog only. My os is Linux - Ubuntu 12.04 - to be specific. I also found that my machine has rsyslog than syslog installed.
我有一个应用程序myapp
应该只将日志文件发送到/var/log/myapp.log
. myapp
是用C++写的。以下示例代码仅将日志发送到 /var/log/syslog。我的操作系统是 Linux - Ubuntu 12.04 - 具体来说。我还发现我的机器安装了 rsyslog 而不是 syslog。
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
int main(void) {
openlog("myapp", LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO, "abc 10");
closelog();
return 0;
}
采纳答案by lupz
According to the syslog(3) manpage, the first parameter for openlog() sets a prefix for log messages, not a filename. You can use a facility like LOG_LOCAL0 to flag your output and then configure syslogd using /etc/syslog.confto send those logs to the file of your desire.
根据 syslog(3) 联机帮助页, openlog() 的第一个参数设置日志消息的前缀,而不是文件名。您可以使用类似 LOG_LOCAL0 的工具来标记您的输出,然后使用 /etc/syslog.conf 配置 syslogd将这些日志发送到您想要的文件。