登录Linux的 [英] Logging in Linux

查看:121
本文介绍了登录Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个Linux系统上运行的后台程序,我想拥有它的活动的记录:日志。现在的问题是,什么是最好的方式来做到这一点?

So I have a daemon running on a linux system, and I want to have a record of its activities: a log. The question is, what is the "best" way to accomplish this?

我的第一个想法是只需打开一个文件,并写入了。

My first idea is to simply open a file and write to it.

FILE* log = fopen("logfile.log", "w");
/* daemon works...needs to write to log */
fprintf(log, "foo%s\n", (char*)bar);
/* ...all done, close the file */
fclose(log);

这有什么内在的错误日志这种方式?有没有更好的办法,如Linux内置的一些框架?

Is there anything inherently wrong with logging this way? Is there a better way, such as some framework built into linux?

推荐答案

Unix的已半晌一个特殊的日志框架称为系统日志。键入你的shell

Unix has had for a long while a special logging framework called syslog. Type in your shell

man 3 syslog

和你会得到C接口的帮助吧。

and you'll get the help for the C interface to it.

一些

#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {

 openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
 syslog(LOG_INFO, "A different kind of Hello world ... ");
 closelog();

 return 0;
}

这篇关于登录Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆