linux c syslog函数

2021-06-03, updated 2021-09-12

openlog, syslog, closelog是一套系统日志写入接口。

1
2
3
4
5
6
7
#include <syslog.h>

void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);

void vsyslog(int priority, const char *format, va_list ap);

openlog()是一个可选项,如果没有使用openlog(),会在使用syslog()时自动调用,ident将会是NULL

syslog()是用来生成log,使用这个log由syslogd管理

closelog()用来关闭用来写syslog的文件描述符,是一个可选项。

1
2
3
4
5
6
7
8
9
#include <syslog.h>
int main(int argc, char **argv) {
    openlog("zooyo", LOG_CONS | LOG_PID, 0);
    syslog(LOG_INFO,
            "This is a syslog test message generated by program %sn",
            argv[0]);
    closelog();
    return 0;
}

编译生成可执行程序后,运行一次程序将向/var/log/message文件添加一行信息如下:

1
Mar  2 16:17:52 ubuntu zooyo[1380]: This is a syslog test message generated by program ./a.out

openlog函数:

参数

Syslog为每个事件赋予几个不同的优先级:

参考:https://www.cnblogs.com/rohens-hbg/articles/5029399.html

words: 1136 tags: linux c