- 目的
由于经常会使用到日志打印,但是网友们大部分说的也不清楚和实用,我这直接给出最浅显易懂,比较实用的一种分享和学习笔记吧。
目录
1.下载安装
tao@ubuntu:~/study/test$ git clone https://github.com/HardySimpson/zlog.git
正克隆到 'zlog'...
remote: Enumerating objects: 3264, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3264 (delta 0), reused 0 (delta 0), pack-reused 3261
接收对象中: 100% (3264/3264), 3.16 MiB | 4.76 MiB/s, 完成.
处理 delta 中: 100% (2329/2329), 完成.
- 切换到下载的zlog目录
tao@ubuntu:~/study/test$ ls
zlog
tao@ubuntu:~/study/test$ cd zlog/
tao@ubuntu:~/study/test/zlog$ ls
Changelog COPYING doc INSTALL makefile README.md src test TODO tools
- 安装到系统
$ make
$ sudo make install
2.介绍
- 使用方法:代码+配置文件
因为打印可以配置格式,索引zlog库的使用就需要一个zlogname.conf的配置文件,在配置文件里面可以指定打印格式,等级,等等。。。
- myzlog.conf配置文件介绍
由于配置文件格式比较丰富,条件也很多,所以,我在这里只提供一个最简单,最实用的配置文件可直接使用,想要详细了解可在最下面配置文件格式详细介绍。
myzlog.conf文件是将打印日志输出到mytest.log文件中,可在mytest.conf文件查看日志。
- 打印等级
#默认打印等级: FATAL > ERROR > WARN > NOTICE > INFO > DEBUG
- 常用myzlog.conf配置文件内容
[global]
strict init = true
buffer min = 1024
buffer max = 2MB
rotate lock file = /tmp/myzlog.lock
default format = "%d.%us [%-6V] [%p:%F:%L] - %m%n"
file perms = 600#默认打印等级: FATAL > ERROR > WARN > NOTICE > INFO > DEBUG
#只有当打印等级大于等于DEBUG时才会打印输出#定义的myrule_class将会在函数dzlog_init("myzlog.conf", "myrule_class")中使用
[rules]
myrule_class.DEBUG "mytest.log",10kb * 3 ~ "mytest.txt.#r"; #输出到mytest.log文件
# my_zlog.DEBUG >stdout; #打印输出到终端
3.zlog库代码使用案例
- 测试目录结构

- 测试代码
#include <stdio.h>
#include "zlog.h"
int main(void)
{
int ret;
int d=10;
char *s="world";
ret = dzlog_init("myzlog.conf", "myrule_class"); //指定配置文件路径及类型名 初始化zlog
if (ret)
{
printf("init failed\n");
return -1;
}
//打印等级数值越低,等级越高
dzlog_debug("debug"); //5
dzlog_info("info, %d %s",d,s); //4
dzlog_notice("notice"); //3
dzlog_warn("warn"); //2
dzlog_error("error"); //1
dzlog_fatal("fatal"); //0
zlog_fini(); //释放zlog
return 0;
}
- 编译
gcc main.c -lzlog
此时会在zlog_test目录下产生mytest.log日志文件,查看日志文件默认会输出所有打印,因为在myzlog.conf配置文件配置的是 myrule_class.DEBUG,会打印等级>=DEBUG等级的打印。
- 查看日志文件
tao@ubuntu:~/study/zlog_test$ ls
a.out main.c mytest.log myzlog.conf
tao@ubuntu:~/study/zlog_test$ cat mytest.log
2021-08-25 01:31:26.355455 [DEBUG ] [102492:main.c:16] - debug
2021-08-25 01:31:26.355504 [INFO ] [102492:main.c:17] - info, 10 world
2021-08-25 01:31:26.355515 [NOTICE] [102492:main.c:18] - notice
2021-08-25 01:31:26.355522 [WARN ] [102492:main.c:19] - warn
2021-08-25 01:31:26.355528 [ERROR ] [102492:main.c:20] - error
2021-08-25 01:31:26.355534 [FATAL ] [102492:main.c:21] - fatal
到这里基本就完美的结束啦!!!
4.详细配置文件格式解释
• strict init
如果"strict init"是true,zlog_init()将会严格检查所有的格式和规则,任何错误都会导致zlog_init() 失败并且返回-1。当"strict init"是false的时候,zlog_init()时会忽略错误的格式和规则。这个参数默认为true
• buffer min
• buffer max
zlog在堆上为每个线程申请缓存。"buffer min"是单个缓存的最小值,zlog_init()的时候申请这个长度的内存。写日志的时候,如果单条日志长度大于缓存,缓存会自动扩充,直到到"buffer max"。单条日志再长超过"buffer max"就会被截断。如果"buffer max" 是0,意味着不限制缓存,每次扩充为原先的2倍,直到这个进程用完所有内存为止。缓存大小可以加上KB, MB 或GB这些单位。默认来说"buffer min"是1K , "buffer max" 是2MB。(其单位不区分大小写)
• rotate lock file
这个选项指定了一个锁文件,用来保证多进程情况下日志安全转档。zlog会在zlog_init()时候以读写权限打开这个文件。确认你执行程序的用户有权限创建和读写这个文件。• default format
这个参数是缺省的日志格式,默认值为:"%d %V [%p:%F:%L] %m%n"
这种格式产生的输出类似这样:2012-02-14 17:03:12 INFO [3758:test_hello.c:39] hello, zlog
本文提供了一个关于zlog库的简单易懂的使用教程,包括下载安装、配置文件解析、代码示例和详细配置说明。通过示例代码展示了如何在C程序中初始化zlog,配置日志输出等级,并解释了配置文件中的各项参数,帮助读者快速理解和应用zlog库进行日志管理。
1242

被折叠的 条评论
为什么被折叠?



