• C语言日志库zlog基本使用


    1、zlog的下载、编译与安装

    git clone https://github.com/HardySimpson/zlog.git 

    vim src/makefile

    设置安装路径PREFIX

    设置工具链CC

    编译问题:

    relocation R_X86_64_PC32 against symbol `zlog_conf_del'......recompile with –fPIC

    解决方法:REAL_CFLAGS之前增加CFLAGS += -fPIC

    make

    make install

    2、使用手册

    http://hardysimpson.github.io/zlog/UsersGuide-CN.html

    3、参考配置

    1. [global]
    2. strict init = true
    3. reload conf period = 0
    4. buffer min = 1024
    5. buffer max = 2MB
    6. rotate lock file = self
    7. default format = "%d(%F %T.%l) %-6V (%c:%F:%L) - %m%n"
    8. file perms = 666
    9. fsync period = 1K
    10. [levels]
    11. #TRACE = 10
    12. #CRIT = 130, LOG_CRIT
    13. [formats]
    14. simple = "%m%n"
    15. normal = "%d [%V] [%U:%L] %m%n"
    16. [rules]
    17. test.* >stdout; normal
    18. test_log.* "./test.log", 1KB*4 ~ "./test-%d(%Y%m%d).#2s.log"; normal

    4、参考demo

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <unistd.h>
    4. #include "zlog.h"
    5. int main(int argc, const char *argv[])
    6. {
    7. int rc;
    8. zlog_category_t *c;
    9. rc = zlog_init("./zlog.conf");//初始化
    10. if (rc)
    11. {
    12. printf("init failed\n");
    13. return -1;
    14. }
    15. c = zlog_get_category("test_log");//获取test_log分类和对应的规则
    16. if (!c)
    17. {
    18. printf("zlog_get_category failed\n");
    19. zlog_fini();
    20. return -2;
    21. }
    22. int i = 7;
    23. zlog_info(c, "abcdecghilklmnopqrstuvwxyz:%d", i);//写日志
    24. zlog_fini();//清理
    25. return 0;
    26. }

    5、说明

    [rules]

    test_log.DEBUG    "./test.log", 1KB*4 ~ "./test-%d(%Y%m%d).#2s.log"; normal

    test_log:分类

    DEBUG:日志等级

    "./test.log", 1KB*4 ~ "./test-%d(%Y%m%d).#2s.log"; normal:具体规则

    "./test.log":日志保存路径

    1KB:每个日志文件的大小

    4:最多保存4个日志文件

    "./test-%d(%Y%m%d).#2s.log":文件转档设置(当日志文件满1KB时进行转档保存)

    normal:设置的日志输出格式

  • 相关阅读:
    【无标题】
    ROS2——初识ROS2(一)
    STM32,复位和时钟控制
    Spring - BeanFactoryPostProcessor 扩展接口
    选择振弦采集仪进行岩土工程监测时,根据不同工况选择合适的种类
    inux安装软件命令yum,apt-get
    elasticsearch11-实战搜索和分页
    泰迪智能科技大数据实训平台分类
    lstat,fstat,unmask,chmod,chown,文件截断函数,空洞文件
    RabbitMQ的安装
  • 原文地址:https://blog.csdn.net/dongyoubin/article/details/125562426