• spdlog 日志库部分源码说明——让你可以自定义的指定自动切换日志时间


    前言

    针对 网络上spdlog日志库目前存在的使用方式固定,不能发挥这个库本身应有价值的情况,这里对一些支持场景进行说明,以供初学者省去阅读源码的时间,直接上手使用

    涉及源码

    在说明过程中使用spdlog库自身提供的使用说明示例,进行介绍。
    源码路径:spdlog-1.x/example/example.cpp

    让日志文件可以每日定时切换 daily_logger_mt()

    template<typename Factory = spdlog::synchronous_factory>
    inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0,
        bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
    {
        return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    介绍:涉及文件:/usr/local/include/spdlog/sinks/daily_file_sink.h 这里是已经安装完成之后的路径。

    接口介绍:

    // 使用给定的工厂类创建一个每日日志记录器
    // 如果没有提供工厂类,则默认为spdlog::synchronous_factory
    template<typename Factory = spdlog::synchronous_factory>
    inline std::shared_ptr<logger> daily_logger_mt(
        const std::string &logger_name,     // 日志记录器的名称
        const filename_t &filename,         // 每日日志文件的名称模式
        int hour = 0,                        // 每天轮换日志文件的时间(小时),默认为0
        int minute = 0,                      // 每天轮换日志文件的时间(分钟),默认为0
        bool truncate = false,               // 如果为true,当日志文件存在时,将其截断;否则,追加到现有文件,默认为false
        uint16_t max_files = 0,              // 保存的最大日志文件数量,默认为0(无限制)
        const file_event_handlers &event_handlers = {} // 文件事件处理器
    )
    {
        // 使用提供的工厂类创建一个每日日志记录器,并返回其共享指针
        return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    使用方式:

    #include "spdlog/sinks/daily_file_sink.h"  // 如果以自己解决方案安装时使用
    // #include  // 如果以默认方式安装时使用
    void daily_example()
    {
        // Create a daily logger - a new file is created every day on 2:30am.
        auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    分享一个有趣的 学习链接:https://xxetb.xet.tech/s/HY8za

  • 相关阅读:
    《痞子衡嵌入式半月刊》 第 62 期
    3 步排查,3 步优化,探针性能损耗直降 44%
    iOS代码混淆-从入门到放弃
    java学习第八天笔记-方法165-文字版格斗游戏
    轻量应用云服务器如何部署SpringBoot项目(jar包形式)?
    python批量修改文件夹下的后缀名
    iOS App更换图标Logo(本地更换)
    量子信息基础知识与实践指南
    算法数据结构体系学习 第十一节
    招聘程序员(软件开发工程师),如何做岗位胜任力测评?
  • 原文地址:https://blog.csdn.net/qq_29111047/article/details/138074286