zlog 是一个高性能、线程安全、灵活、概念清晰的纯 C 日志函数库。
下载 https://github.com/HardySimpson/zlog/releases/tag/1.2.17中的zlog-1.2.17.tar.gz文件
解压, 安装
$ tar -zxvf zlog-1.2.17.tar.gz
$ cd zlog-1.2.17/
$ ./configure --enable-test # 把测试程序和案例也编译下,反正不安装
$ make
$ sudo make install
# 默认安装在/usr/local/下,包括libzlog.so和zlog.h
zlog里面有三个重要的概念,category,format,rule
现在试着写配置文件,配置文件名无所谓,放在哪里也无所谓,反正在zlog_init()的时候可以指定
$ cat /etc/zlog.conf
[formats]
simple = "%m%n"
[rules]
my_cat.DEBUG >stdout; simple
在目前的配置文件的例子里面,可以看到my_cat分类,>=debug等级的日志会被输出到stdout(标准输出),并且输出的格式是simple这个格式,也就是用户输入信息+换行符。如果要输出到文件并控制文件大小为1兆,规则的配置应该是
my_cat.DEBUG "/var/log/aa.log", 1M; simple
test_hello.c代码如下
#include <stdio.h>
#include "zlog.h"
int main(int argc, char** argv)
{
int rc;
zlog_category_t *c;
rc = zlog_init("/etc/zlog.conf");
if (rc) {
printf("init failed\n");
return -1;
}
c = zlog_get_category("my_cat");
if (!my_cat) {
printf("get cat fail\n");
zlog_fini();
return -2;
}
ZLOG_INFO(c, "hello, zlog");
zlog_fini();
return 0;
}
$ gcc -c -o test_hello.o test_hello.c -I/usr/local/include
$ gcc -o test_hello test_hello.o -L/usr/local/lib -lzlog
$ ./test_hello
hello, zlog