C Syslog.h不写日志
Stackoverflow 黑客们大家好!
我有一个非常小的严重问题,或者我和 C syslog() 函数之间的误解。
代码编译得很好,我可以看到它正在执行“虚拟工作”(ping 8.8.8.8),但定义的日志只是不附加。我对此感到完全困惑,不知道出了什么问题。已经有 SMAO(搜遍了我的屁股 - 试图普及它),但就是无法让它正常工作。
代码在这里:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
// Application settings # TODO: import these from a .ini file
#define WORKDIR "/var/run/management"
#define LOGDIR "/var/log/management"
#define LOGFILE "/var/log/management.log"
#define SCRIPTDIR "/var/spool/management"
#define PIDFILE "/var/run/management/daemon.pid"
int main(void) {
printf("Management Daemon\nInitializing...");
pid_t pid, sid;
setlogmask(LOG_UPTO (LOG_NOTICE));
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Management Daemon started by User %d", getuid());
closelog();
printf("Done.\nForking...\n");
pid = fork();
if(pid < 0) {
printf("Fork failed! Exiting...\n");
// TODO: syslog facility
syslog(LOG_EMERG, "Forking failed, exiting.");
closelog();
exit(EXIT_FAILURE);
}
if(pid > 0) {
FILE *pidfile;
pidfile = fopen(PIDFILE, "w");
fprintf(pidfile, "%d\n", pid);
fclose(pidfile);
printf("PID written to %s\nUsing log file %s\nGoing silent...\n", PIDFILE, LOGFILE);
// TODO: syslog facility
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Fork returned with valid PID: %d. PID file: %s", pid, PIDFILE);
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if(sid < 0) {
// TODO: syslog facility
printf("SID is corrupt\n");
exit(EXIT_FAILURE);
}
if(sid > 0) {
printf("Acquired valid SID!\n");
}
if((chdir(WORKDIR)) < 0) {
// TODO: syslog facility
printf("Directory change failed. Got permissions?\n");
exit(EXIT_FAILURE);
}
// Going Silent
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// daemon init here
FILE *fp;
// The big loop
closelog();
while(1) {
// Do your thing...
// TODO: implement daemon executing mechanics.
ret = system("ping 8.8.8.8 -c 1");
fp = fopen("/var/run/management.output", "a");
if(ret == 0) {
fprintf(fp, "Success!\n");
fclose(fp);
}
if(ret == 512) {
fprintf(fp, "Failure!\n");
fclose(fp);
}
// Sleep till the next heartbeat
// TODO: notice level log about heartbeats if verbosity is set to high
sleep(30);
}
exit(EXIT_SUCCESS);
}
我们将非常感谢所有帮助!
estol
解决方案:
在 syslog-ng.conf 中添加以下行:
destination d_management { file("/var/log/management/management.log"); };
filter f_management { match("MD:" value("MESSAGE")); };
log { source(src); filter(f_management); destination(d_management); };
所有包含 MD: 序列的日志消息将被重定向到 management.log 文件。 就像魅力一样。再次感谢您为我指明了正确的方向。
Hi there Stackoverflow hackers!
I have a very minor case of serious problem, or misunderstanding between me, and the C syslog() function.
The code compiles just fine, and I can see its doing its "dummy job" (pinging 8.8.8.8), but the defined log just does not append. I am completly puzzled by this, and have no idea what could be wrong. Already SMAO (Searched My Ass Off - trying to popularize that) but just can't get it working properly.
Code here:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
// Application settings # TODO: import these from a .ini file
#define WORKDIR "/var/run/management"
#define LOGDIR "/var/log/management"
#define LOGFILE "/var/log/management.log"
#define SCRIPTDIR "/var/spool/management"
#define PIDFILE "/var/run/management/daemon.pid"
int main(void) {
printf("Management Daemon\nInitializing...");
pid_t pid, sid;
setlogmask(LOG_UPTO (LOG_NOTICE));
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Management Daemon started by User %d", getuid());
closelog();
printf("Done.\nForking...\n");
pid = fork();
if(pid < 0) {
printf("Fork failed! Exiting...\n");
// TODO: syslog facility
syslog(LOG_EMERG, "Forking failed, exiting.");
closelog();
exit(EXIT_FAILURE);
}
if(pid > 0) {
FILE *pidfile;
pidfile = fopen(PIDFILE, "w");
fprintf(pidfile, "%d\n", pid);
fclose(pidfile);
printf("PID written to %s\nUsing log file %s\nGoing silent...\n", PIDFILE, LOGFILE);
// TODO: syslog facility
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Fork returned with valid PID: %d. PID file: %s", pid, PIDFILE);
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if(sid < 0) {
// TODO: syslog facility
printf("SID is corrupt\n");
exit(EXIT_FAILURE);
}
if(sid > 0) {
printf("Acquired valid SID!\n");
}
if((chdir(WORKDIR)) < 0) {
// TODO: syslog facility
printf("Directory change failed. Got permissions?\n");
exit(EXIT_FAILURE);
}
// Going Silent
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// daemon init here
FILE *fp;
// The big loop
closelog();
while(1) {
// Do your thing...
// TODO: implement daemon executing mechanics.
ret = system("ping 8.8.8.8 -c 1");
fp = fopen("/var/run/management.output", "a");
if(ret == 0) {
fprintf(fp, "Success!\n");
fclose(fp);
}
if(ret == 512) {
fprintf(fp, "Failure!\n");
fclose(fp);
}
// Sleep till the next heartbeat
// TODO: notice level log about heartbeats if verbosity is set to high
sleep(30);
}
exit(EXIT_SUCCESS);
}
All help would be highly appreciated!
estol
The solution:
Added the following lines to syslog-ng.conf:
destination d_management { file("/var/log/management/management.log"); };
filter f_management { match("MD:" value("MESSAGE")); };
log { source(src); filter(f_management); destination(d_management); };
All log messages that contains the MD: sequence, will be redirected to the management.log file.
Works like a charm. Thanks again for pointing me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
openlog() 的第一个参数是程序标识符,而不是日志文件名。这就解释了为什么您在
/var/log/management.log
中找不到任何内容。日志文件的名称通常在记录器守护程序的配置文件中设置。该文件的名称和位置取决于您使用的守护程序(例如,在我的计算机上它是
/etc/syslog-ng/syslog-ng.conf
)。The first argument to openlog() is a program identifier, not a log file name. That explains why you won't find anything in
/var/log/management.log
.The name of the log file is usually set in the logger daemon's configuration file. The name and location of that file depend on the daemon you're using (e.g. it's
/etc/syslog-ng/syslog-ng.conf
on my machine).