在使用Poco库开发应用的时候,需要使用日志模块记录程序运行的一些信息。这里介绍一下日志模块的常见用法。

1.获取固定名称的日志类

Poco::Logger & logger = Poco::Logger::get("MyApplication");

2.格式化日志的输出内容

//@1设置日志输出的文件地址
//output 文件格式
AutoPtr<FormattingChannel>  GetLogFormatChannel(std::string log_file_path)
{//添加对应的日志输出通道AutoPtr<FileChannel> pChannel(new FileChannel);std::string logFilePath = log_file_path;//日志的文件路径pChannel->setProperty("path", logFilePath);//是否切换文件(防止单一文件太大)//这里选否,意思是所有日志内容输出到一个文件里面pChannel->setProperty("rotation", "never");//文件切换的时候追加的后缀类型//可以是数字number也可以是时间timestamp pChannel->setProperty("archive", "timestamp");//是否对文件内容进行压缩pChannel->setProperty("compress", "false");//定时清理日志文件///* <n> [seconds]///* <n> minutes///* <n> hours///* <n> days///* <n> weeks///* <n> months (a month 30 days)//10天清理一次文件pChannel->setProperty("purgeAge", "10 days");//日志输出的格式%Y(年格式1970)%m(月格式01~12)%d(日格式01~31)//小时格式%H(00~23)分钟格式%M(00~59) 秒钟格式%S(00~59)//日志严重程度%p(Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace)//消息源%s  日志文本%tAutoPtr<PatternFormatter> pPF(new PatternFormatter("%Y-%m-%d %L%H:%M:%S %p %s:%t"));AutoPtr<FormattingChannel> pFC(new FormattingChannel(pPF, pChannel));return pFC;
}Poco::Logger & logger = Poco::Logger::get("MyApplication");
//设置对应的输出通道
AutoPtr<FormattingChannel>  log_channel = GetLogFormatChannel(log_file_path);
//在日志类中添加日志文件地址和日志的输出格式
logger.setChannel(log_channel);

3.使用宏输出对应的日志内容

Poco::Logger & logger = Poco::Logger::get("MyApplication");//输出不带变量的日志内容
poco_information(logger, "task start");
poco_error(logger, "run tools failed");//输出带一个参数的日志内容
poco_error_f1(logger, "throw exception:%s", "test");//输出带两个参数的日志内容
poco_information_f2(logger, "type is:%s,return:%d", "test", 20);

4.采用独立线程输出日志内容

//定义独立线程
class LogRunnable : public Runnable
{public:LogRunnable(AutoPtr<AsyncChannel> pAsync):_pAsync(pAsync),_stop(false){}void run(){Message msg;while (!_stop) _pAsync->log(msg);}void stop(){_stop = true;}
private:AutoPtr<AsyncChannel> _pAsync;std::atomic<bool> _stop;
};
//在独立线程里面运行日志
int run_log_in_thread()
{//定义异步管道AutoPtr<ConsoleChannel> pChannel = new ConsoleChannel;AutoPtr<AsyncChannel> pAsync = new AsyncChannel(pChannel);//管道放到线程里面LogRunnable log_runner(pAsync);pAsync->open();//运行异步线程Thread log_thread;log_thread.start(log_runner);//输出日志内容Message msg;pAsync->log(msg);pAsync->close();//停止执行log_runner.stop();log_thread.join();int msg_size = pChannel->list().size();
}

5.输出日志内容到终端

//自定义格式化类
class SimpleFormatter: public Formatter
{public:void format(const Message& msg, std::string& text){text = msg.getSource();text.append(": ");text.append(msg.getText());}
};//设置终端输出管道
AutoPtr<ConsoleChannel> pChannel = new ConsoleChannel;
AutoPtr<Formatter> pFormatter = new SimpleFormatter;
AutoPtr<FormattingChannel> pFormatterChannel = new FormattingChannel(pFormatter, pChannel);//输出日志消息
Message msg("Source", "Text", Message::PRIO_INFORMATION);
pFormatterChannel->log(msg);

6.日志内容同时输出给多个管道

AutoPtr<TestChannel> pChannel = new TestChannel;
//SplitChannel支持同时输出日志内容到多个管道
AutoPtr<SplitterChannel> pSplitter = new SplitterChannel;
pSplitter->addChannel(pChannel);
pSplitter->addChannel(pChannel);
//写日志
Message msg;
pSplitter->log(msg);

7.输出日志内容到流中

//自定义格式化类
class SimpleFormatter: public Formatter
{public:void format(const Message& msg, std::string& text){text = msg.getSource();text.append(": ");text.append(msg.getText());}
};
//指定流管道
std::ostringstream str;
AutoPtr<StreamChannel> pChannel = new StreamChannel(str);//使用自定义的格式化类
AutoPtr<Formatter> pFormatter = new SimpleFormatter;
AutoPtr<FormattingChannel> pFormatterChannel = new FormattingChannel(pFormatter, pChannel);//输出日志内容到字符流
Message msg("Source", "Text", Message::PRIO_INFORMATION);
pFormatterChannel->log(msg);
//assertTrue (str.str().find("Source: Text") == 0);

Poco库使用:日志模块相关推荐

  1. C++ 使用Poco库实现日志操作

    C++ 使用Poco库实现日志操作 flyfish 文章目录 C++ 使用Poco库实现日志操作 日志输出到文件 日志输出到控制台 日志同时输出到文件和控制台 示例:将异常输出到日志 日志输出到文件 ...

  2. POCO库 Foundation::Thread模块 多线程与线程池支持

    本节主要介绍Thread类和ThreadLocal机制的使用方法以及实现原理,以及对ThreadPool线程池支持的简单了解 在C++语言中,我们通过_beginThreadex或CreateThre ...

  3. 微软企业库4.1学习笔记(三十六)日志模块 简介

    日志模块 企业库的日志模块简单的实现了日志功能的常用功能.开发者可以利用模块在下面的位置记录信息: 事件日志 电子邮件 数据库 消息队列 文本文件 WMI的事件查看器 自定义的位置 模块为记录在任何位 ...

  4. C++ Poco库的PC编译和交叉编译

    C++ Poco库的PC编译和交叉编译 flyfish 文章目录 C++ Poco库的PC编译和交叉编译 环境 源码下载地址 PC编译 编译依次执行 CMakeLists.txt文件配置 代码实现 P ...

  5. 微信终端跨平台组件 mars 系列(一) - 高性能日志模块xlog

    前言 mars 是微信官方的终端基础组件,是一个使用 C++ 编写的业务性无关,平台性无关的基础组件.目前已接入微信 Android.iOS.Mac.Windows.WP 等客户端.现正在筹备开源中, ...

  6. python日志模块_Python之日志处理(logging模块)

    转载自:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logg ...

  7. 日志模块-logging模块

    日志模块 -logging(非常重要) 记录用户行为 程序运行过程 程序错误记录 logging.debug()通常调试时用到的日志信息 logging.info() #证明事情按照预期的那样工作 l ...

  8. python标准库对象导入语句_Python标准库之Sys模块使用详解

    sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. 使用sy ...

  9. 基于 C++ POCO 库封装的异步多线程的 CHttpClient 类

    用惯了 Jetty 的 基于事件的 HttpClient 类,在C++平台上也没找到这样调用方式的类库,只好自己写一个了. 目前版本 1.0,朋友们看了给点建议.(注:Kylindai原创,转载请注明 ...

最新文章

  1. 架构师之路 — 分布式系统 — CAP 定理
  2. 30分钟LINQ教程
  3. P1982 小朋友的数字
  4. TWAIN协议学习笔记
  5. Linux C 实现一个简单的线程池
  6. 外部服务发现之 ingress(一) traefik 的安装使用
  7. 【MySQL】MySQL运维及开发规范
  8. rhel系统启动过程_Linux系统启动过程
  9. 在鉴定名画真伪这件事上,专家可能要被AI代替了
  10. cocos2dx 2.0升级为3.0一些常见变化纪录
  11. 通信中的“交织”技术
  12. html如何插入下拉菜單,html下拉菜单怎么做?html下拉菜单的代码实例介绍
  13. Unity---商店搭建
  14. 【JZOJ 5421】【NOIP2017提高A组集训10.25】嘟嘟噜
  15. 各种水龙头拆卸图解_各种水龙头拆卸图解 蜜罐蚁小编带您了解水龙头拆卸方法...
  16. python从0到1_从0到1的Python学习经验
  17. keep-alive 的详细介绍
  18. EXCEL 字符替换为换行符
  19. Day001--Scala中的下载安装配置及下载安装集成开发环境IDEA
  20. 计算机右下角时间格式,电脑右下角时间格式_电脑右下角时间不准

热门文章

  1. python千位分隔符_python – 如何设置自定义千位分隔符?
  2. 论文阅读:SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
  3. [区块链笔记9] 区块链相关概念
  4. atop用法_Linux atop 命令 command not found atop 命令详解 atop 命令未找到 atop 命令安装 - CommandNotFound ⚡️ 坑否...
  5. 刘克峰:知识,技巧与想象力
  6. ESXi 直通 k80 GPU到Win10
  7. 采购管理-输入、输出、工具和技术
  8. 如何设置积分兑换电子优惠券规则?
  9. 机器学习小型数据集_小型计算机与新机器的灵魂
  10. 关联多宝平台容器不支持这种初始化