问题描述
我有一个应用程序 myapp
,该应用程序应将日志文件仅 发送到/var/log/myapp.log
. myapp
用C ++编写.以下示例代码仅将日志发送到/var/log/syslog.具体来说,我的操作系统是Linux-Ubuntu 12.04.我还发现我的计算机上安装的rsyslog大于syslog.
I have an application myapp
which should send log files only to /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.
#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;
}
推荐答案
根据 syslog(3) 联机帮助页,openlog() 的第一个参数设置日志消息的前缀,而不是文件名.您可以使用LOG_LOCAL0之类的工具来标记输出,然后使用/etc/syslog.conf 配置syslogd将这些日志发送到您想要的文件.
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.conf to send those logs to the file of your desire.
这篇关于如何配置系统日志,以便应用程序日志转到特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!