简体   繁体   English

如何配置syslog以便应用程序日志转到特定文件

[英]How to configure syslog so that an applications log goes to a specific file

I have an application myapp which should send log files only to /var/log/myapp.log . 我有一个应用程序myapp ,它应该将日志文件发送到/var/log/myapp.log myapp is written in C++. myapp是用C ++编写的。 The following sample code, sends the logs to /var/log/syslog only. 以下示例代码仅将日志发送到/ var / log / syslog。 My os is Linux - Ubuntu 12.04 - to be specific. 我的操作系统是Linux - Ubuntu 12.04 - 具体而言。 I also found that my machine has rsyslog than syslog installed. 我还发现我的机器安装了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;
}

According to the syslog(3) manpage, the first parameter for openlog() sets a prefix for log messages, not a filename. 根据syslog(3)联机帮助页,openlog()的第一个参数为日志消息设置前缀,而不是文件名。 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. 您可以使用LOG_LOCAL0之类的工具标记输出,然后使用/etc/syslog.conf配置syslogd ,将这些日志发送到您想要的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM