美文网首页
2.使用log4cpp记录日志

2.使用log4cpp记录日志

作者: Pokerpoke | 来源:发表于2017-09-12 09:38 被阅读0次

简介

log4cpp是个基于LGPL的开源项目,是基于优秀的日志处理跟踪项目Java语言的log4j移植过来的。

优点

  • 提供应用程序运行上下文,方便跟踪调试
  • 可扩展的、多种方式记录日志,包括命令行、文件、回卷文件、内存、syslog服务器、Win事件日志等
  • 可以动态控制日志记录级别,在效率和功能中进行调整
  • 所有配置可以通过配置文件进行动态调整
  • 多语言支持,包括Java(log4j),C++(log4cpp、log4cplus),C(log4c),python(log4p)等

配置及使用

  1. 包含头文件
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
  1. 初始化附加目的地(appenders)
// 输出到std::cout
log4cpp::Appender *appender = new log4cpp::OstreamAppender("root", &std::cout);
// 输出到log文件
log4cpp::Appender *appender = new log4cpp::FileAppender("root", "test.log");
  1. 设置输出格式(layout)
log4cpp::PatternLayout *patternLayout = new log4cpp::PatternLayout();
patternLayout->setConversionPattern("%d [%p] - %m%n");
appender->setLayout(patternLayout);

其他相关可以自己设置相关提示信息

  • %% - a single percent sign
  • %c - the category
  • %d - the date\n Date format: The date format character may be followed by a date format specifier enclosed between braces. For example, %d{%H:%M:%S,%l} or %d{%d %m %Y %H:%M:%S,%l}. If no date format specifier is given then the following format is used: "Wed Jan 02 02:03:55 1980". The date format specifier admits the same syntax as the ANSI C function strftime, with 1 addition. The addition is the specifier %l for milliseconds, padded with zeros to make 3 digits.
  • %m - the message
  • %n - the platform specific line separator
  • %p - the priority
  • %r - milliseconds since this layout was created.
  • %R - seconds since Jan 1, 1970
  • %u - clock ticks since process start
  • %x - the NDC
  • %t - thread name
  1. 设置类别(categories)和优先级(priority)
log4cpp::Category &root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::NOTICE);
root.addAppender(appender);
  • 优先级:NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG
  1. 定义一个宏定义
// 使用 LOG(WARN) << "message" 的格式来记录日志 
#define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << "\n\t" << __FILE__ << " " << __LINE__ << ": "
  1. 在文件中记录打印日志
...
LOG(INFO) << "Device open success.";
...
LOG(ERROR) << "Device open failed.";
...

设置优先级以后只有高于所设置优先级的信息会被记录下来,因此可以通过宏定义来实现Debug和Release不同的信息优先级。

  1. 运行结果示例
2017-09-12 09:31:28,317 [INFO] - 
    /home/jiang/git/aero-node/an-core/example/qa_voice_capture.cc 36: Device open success.

2017-09-12 09:31:28,318 [INFO] - 
    /home/jiang/git/aero-node/an-core/example/qa_voice_capture.cc 59: Initialize hardware parameter success.

2017-09-12 09:31:28,318 [INFO] - 
    /home/jiang/git/aero-node/an-core/example/qa_voice_capture.cc 70: Set access type success.
  1. 其他
  • 可以将初始化配置自己封装到一个logger.h文件中,以后再需要使用直接include即可

相关文章

  • 2.使用log4cpp记录日志

    简介 log4cpp是个基于LGPL的开源项目,是基于优秀的日志处理跟踪项目Java语言的log4j移植过来的。 ...

  • SpringBoot 基于AOP的低侵入式日志

    ​ 使用Springboot Aop 注解的方式 实现 1.声明日志注解 2.用于日志记录的 DO 3.记录日志

  • 日志系统设计

    1. 什么是日志? 在我们项目中,使用日志来记录整个系统的运行情况。可能但是不限于: 2. 日志的目的 记录日志的...

  • 面试的问题(2)

    1. 使用Kafka 如何保证消息有序? 2. 50 业务线程记录日志。一个I/O 线程读日志。如何设计? 思...

  • python logging 同时屏幕输出和保存到文件

    日志记录,不同模块使用不同logger分开记录,同时输出到屏幕和文件日志记录完后,可以使用del 来主动释放日志占...

  • spring event 使用

    使用场景 在开发中有些重要业务需要记录日志并保存.使用spring event 事件发布日志,统一监听日志并记录....

  • 使用SLF4J记录日志

    日志推荐 1、在使用日志时应该使用抽象层接口日志框架来记录日志,方便以后的扩展和更换日志框架; 2、日志...

  • houyi平台-开发指南-日志记录

    日志说明 在需要使用记录日志的java类上使用@Sl4j注解,就可以使用log.xx方法,记录日志了。示例如下: ...

  • JanusGraph---Transaction Log

    事务日志 记录事务日志 添加事务日志,使用addedPerson标识。 事务中改变都会被记录到用户日志系统并以id...

  • 关于centos容器中postfix不记录日志的问题

    1.postfix使用rsyslog记录日志。 2.默认情况下,centos的docker镜像中没有安装rsysl...

网友评论

      本文标题:2.使用log4cpp记录日志

      本文链接:https://www.haomeiwen.com/subject/yzgqsxtx.html