0. What is zlog?
-------------
zlog is a reliable, high-performance, thread safe, flexible, clear-model, pure C logging library.
Actually, in the C world there was NO good logging library for applications like logback in java or log4cxx in c++. Using printf can work, but can not be redirected or reformatted easily. syslog is slow and is designed for system use.
So I wrote zlog.
It is faster, safer and more powerful than log4c. So it can be widely used.
1. Install
-------------
Downloads: https://github.com/HardySimpson/zlog/releases
$ tar -zxvf zlog-latest-stable.tar.gz
$ cd zlog-latest-stable/
$ make
$ sudo make install
or
$ make PREFIX=/usr/local/
$ sudo make PREFIX=/usr/local/ install
PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure your program can find zlog library.
$ sudo vi /etc/ld.so.conf
/usr/local/lib
$ sudo ldconfig
Before running a real program, make sure libzlog.so is in the directory where the system's dynamic lib loader can find it. The command metioned above are for linux. Other systems will need a similar set of actions.
2. Introduce configure file
-------------
There are 3 important concepts in zlog: categories, formats and rules.
Categories specify different kinds of log entries. In the zlog source code, category is a (zlog_cateogory_t *) variable. In your program, different categories for the log entries will distinguish them from each other.
Formats describe log patterns, such as: with or without time stamp, source file, source line.
Rules consist of category, level, output file (or other channel) and format. In brief, if the category string in a rule in the configuration file equals the name of a category variable in the source, then they match. Still there is complex match range of category. Rule decouples variable conditions. For example, log4j must specify a level for each logger(or inherit from father logger). That's not convenient when each grade of logger has its own level for output(child logger output at the level of debug, when father logger output at the level of error)
Now create a configuration file. The function zlog_init takes the files path as its only argument.
$ cat /etc/zlog.conf
[formats]
simple = "%m%n"
[rules]
my_cat.DEBUG >stdout; simple
In the configuration file log messages in the category "my_cat" and a level of DEBUG or higher are output to standard output, with the format of simple(%m - usermessage %n - newline). If you want to direct out to a file and limit the files maximum size, use this configuration
my_cat.DEBUG "/var/log/aa.log", 1M; simple
3. Using zlog API in C source file
-------------
$ vi 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 (!c) {
printf("get cat fail\n");
zlog_fini();
return -2;
}
zlog_info(c, "hello, zlog");
zlog_fini();
return 0;
}
4. Compile, and run it!
-------------
$ cc -c -o test_hello.o test_hello.c -I/usr/local/include
$ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog -lpthread
$ ./test_hello
hello, zlog
5. Advanced Usage
-------------
* syslog model, better than log4j model
* log format customization
* multiple output destinations including static file path, dynamic file path, stdout, stderr, syslog, user-defined output
* runtime manually or automatically refresh configure(safely)
* high-performance, 250'000 logs/second on my laptop, about 1000 times faster than syslog(3) with rsyslogd
* user-defined log level
* thread-safe and process-safe log file rotation
* microsecond accuracy
* dzlog, a default category log API for easy use
* MDC, a log4j style key-value map
* self debuggable, can output zlog's self debug&error log at runtime
* No external dependencies, just based on a POSIX system and a C99 compliant vsnprintf.
6. Links:
-------------
* Homepage: http://hardysimpson.github.io/zlog
* Downloads: https://github.com/HardySimpson/zlog/releases
* Author's Email: HardySimpson1984@gmail.com
* auto tools version: https://github.com/bmanojlovic/zlog
* cmake verion: https://github.com/lisongmin/zlog
* windows version: https://github.com/lopsd07/WinZlog
没有合适的资源?快使用搜索试试~ 我知道了~
zlog 是一个可靠、高性能、线程安全、灵活、模型清晰、纯 C 日志库
共135个文件
c:51个
h:29个
conf:23个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 128 浏览量
2024-03-20
21:39:59
上传
评论
收藏 177KB ZIP 举报
温馨提示
在 C 世界中,没有好的日志库可用于应用程序,例如 java 中的 logback 或 c++ 中的 log4cxx。使用 printf 可以工作,但不能轻易重定向或重新格式化。syslog 速度很慢,专为系统使用而设计。 所以写了zlog。 它比 log4c 更快、更安全、更强大。所以它可以被广泛使用。
资源推荐
资源详情
资源评论
收起资源包目录
zlog 是一个可靠、高性能、线程安全、灵活、模型清晰、纯 C 日志库 (135个子文件)
BUILD 451B
zlog.c 30KB
rule.c 27KB
conf.c 21KB
spec.c 20KB
buf.c 17KB
rotater.c 13KB
category.c 6KB
zc_hashtable.c 6KB
test_multithread.c 5KB
event.c 5KB
thread.c 5KB
format.c 4KB
level_list.c 3KB
category_table.c 3KB
level.c 3KB
mdc.c 3KB
zc_arraylist.c 3KB
zc_util.c 3KB
test_buf.c 3KB
zc_profile.c 2KB
test_hex.c 2KB
zlog_win.c 2KB
test_press_write.c 2KB
test_press_write2.c 2KB
test_press_zlog2.c 1KB
test_press_zlog.c 1KB
record_table.c 1KB
lockfile.c 1KB
zlog-chk-conf.c 1KB
test_hashtable.c 1KB
record.c 1KB
test_longlog.c 1KB
test_init.c 992B
test_leak.c 991B
test_press_syslog.c 960B
test_prompt.c 894B
test_bitmap.c 877B
test_enabled.c 872B
test_mdc.c 780B
test_conf2.c 779B
test_tmp.c 724B
test_category.c 718B
test_conf.c 717B
test_record.c 714B
test_level.c 653B
test_syslog.c 610B
test_hello.c 560B
test_pipe.c 556B
zlog_init_fuzzer.c 524B
test_profile.c 459B
test_default.c 441B
Changelog 5KB
CPackConfig.cmake 3KB
toolchain-mingw64.cmake 1KB
FindUnixem.cmake 704B
LoadLibraries.cmake 677B
test_conf.conf 1KB
zlog.conf 687B
test_press_zlog.conf 598B
test_multithread.conf 521B
test_press_zlog2.conf 406B
test_syslog.conf 314B
test_leak.conf 210B
test_tmp.conf 210B
test_longlog.conf 186B
test_prompt.conf 174B
test_init.2.conf 160B
test_hello.conf 135B
test_init.conf 134B
test_leak.2.conf 110B
test_mdc.conf 107B
test_level.conf 102B
test_enabled.conf 102B
test_category.conf 86B
test_pipe.conf 85B
test_record.conf 78B
test_profile.conf 58B
test_default.conf 28B
test_hex.conf 24B
.gitignore 554B
zlog.h 11KB
zlog_win.h 3KB
event.h 2KB
rule.h 2KB
zc_hashtable.h 2KB
buf.h 2KB
zc_xplatform.h 2KB
spec.h 1KB
zc_profile.h 1KB
test_conf2.conf.h 1KB
category.h 1KB
zc_arraylist.h 1KB
rotater.h 1KB
test_enabled.h 1KB
conf.h 1KB
test_level.h 1KB
thread.h 963B
category_table.h 906B
mdc.h 905B
共 135 条
- 1
- 2
资源评论
新华
- 粉丝: 1w+
- 资源: 629
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功