C++-Poco框架:日志【FileChannel.h、Message.h、FormattingChannel.h】
当我们设计后台程序时,日志是获知程序的唯一的维护手段,所以日志很重要,是程序稳定运行和维护升级的唯一通道。
在我用过的日志框架中,poco提供的日志方法,简单明了,使用方便,很值得推广使用。
下面是使用的例子,主要是先学会简单的使用方法,然后再好好理清这些知识点:
#include <Poco/FileChannel.h>
#include <Poco/Message.h>
#include <Poco/FormattingChannel.h>
using Poco::Logger;
using Poco::PatternFormatter;
using Poco::FormattingChannel;
using Poco::ConsoleChannel;
using Poco::FileChannel;
using Poco::Message;
int main(int argc, char **argv) {
//终端日志记录
FormattingChannel *pFCConsole = new FormattingChannel(new PatternFormatter("%s: %p: %t"));
pFCConsole->setChannel(new ConsoleChannel);
pFCConsole->open();
//文件日志记录
FormattingChannel *pFCFile = new FormattingChannel(new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s:%q:%t"));
pFCFile->setChannel(new FileChannel("sample.log"));
pFCFile->open();
// 建立两种日志的对象
Logger &consoleLogger = Logger::create("ConsoleLogger", pFCConsole, Message::PRIO_INFORMATION);
Logger &fileLogger = Logger::create("FileLogger", pFCFile, Message::PRIO_WARNING);
// 记录错误日志
consoleLogger.error("An error message");
fileLogger.error("An error message");
//记录报警日志
consoleLogger.warning("A warning message");
fileLogger.error("A warning message");
//记录信息日志
consoleLogger.information("An information message");
fileLogger.information("An information message");
//记录信息日志:方法二
poco_information(consoleLogger, "Another informational message");
poco_warning_f2(consoleLogger, "A warning message with arguments: %d, %d", 1, 2);
//记录信息日志:方法三
Logger::get("ConsoleLogger").error("Another error message");
return 0;
}
poco框架:日志相关知识和使用方法_51CTO博客_Poco框架
还没有评论,来说两句吧...