C++-Poco框架:日志【FileChannel.h、Message.h、FormattingChannel.h】

妖狐艹你老母 2023-09-25 12:46 92阅读 0赞

当我们设计后台程序时,日志是获知程序的唯一的维护手段,所以日志很重要,是程序稳定运行和维护升级的唯一通道。

在我用过的日志框架中,poco提供的日志方法,简单明了,使用方便,很值得推广使用。

下面是使用的例子,主要是先学会简单的使用方法,然后再好好理清这些知识点:

  1. #include <Poco/FileChannel.h>
  2. #include <Poco/Message.h>
  3. #include <Poco/FormattingChannel.h>
  4. using Poco::Logger;
  5. using Poco::PatternFormatter;
  6. using Poco::FormattingChannel;
  7. using Poco::ConsoleChannel;
  8. using Poco::FileChannel;
  9. using Poco::Message;
  10. int main(int argc, char **argv) {
  11. //终端日志记录
  12. FormattingChannel *pFCConsole = new FormattingChannel(new PatternFormatter("%s: %p: %t"));
  13. pFCConsole->setChannel(new ConsoleChannel);
  14. pFCConsole->open();
  15. //文件日志记录
  16. FormattingChannel *pFCFile = new FormattingChannel(new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s:%q:%t"));
  17. pFCFile->setChannel(new FileChannel("sample.log"));
  18. pFCFile->open();
  19. // 建立两种日志的对象
  20. Logger &consoleLogger = Logger::create("ConsoleLogger", pFCConsole, Message::PRIO_INFORMATION);
  21. Logger &fileLogger = Logger::create("FileLogger", pFCFile, Message::PRIO_WARNING);
  22. // 记录错误日志
  23. consoleLogger.error("An error message");
  24. fileLogger.error("An error message");
  25. //记录报警日志
  26. consoleLogger.warning("A warning message");
  27. fileLogger.error("A warning message");
  28. //记录信息日志
  29. consoleLogger.information("An information message");
  30. fileLogger.information("An information message");
  31. //记录信息日志:方法二
  32. poco_information(consoleLogger, "Another informational message");
  33. poco_warning_f2(consoleLogger, "A warning message with arguments: %d, %d", 1, 2);
  34. //记录信息日志:方法三
  35. Logger::get("ConsoleLogger").error("Another error message");
  36. return 0;
  37. }

poco框架:日志相关知识和使用方法_51CTO博客_Poco框架

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java 日志框架

    摘要: 在Java中工程中,输出日志需要使用一个或者多个日志框架,这些框架提供了必要的对象、方法和配置来传输消息。 java.util.logging 在JDK 1.4

    相关 日志框架

    日志门面与日志框架 slf4j是典型的“日志门面(Logging Facade)”,利用了设计模式中的[门面模式][Link 1]思想,对外提供一套通用的日志记录的API

    相关 java日志框架

    前言 日志在软件系统的各个方面都有非常重要地位,没有日志虽然不会影响系统的正确运行,但是当系统某个部分模块或者某个小小的组件出了问题之后,没有日志的生活是这样的:有问题现

    相关 日志框架浅析

                                    1. 日志简介     在Java项目中,日志是必不可少的功能,日志对于快速定位问题,检查日常项目运行状