引言
日志对于程序来说是非常重要的,特别是对一些大型程序而言。一旦程序被发布,在现场日志几乎是程序员唯一可以获取程序信息的手段。
POCO使用Poco::Message 对象存储和传递日志信息。
#include “Poco/Message.h”
日志消息(Message)包括:
此处只列出一个,具体查看文档
优先级(priority)
POCO的定义八个消息优先级:
- PRIO_FATAL (最高优先级)
- PRIO_CRITICAL
- PRIO_ERROR
- PRIO_WARNING
- PRIO_NOTICE
- PRIO_INFORMATION
- PRIO_DEBUG
- PRIO_TRACE (最低优先级)
**API接口:**
//可以通过函数设置和获取消息优先级:
void setPriority(Priority prio)
Priority getPriority() const
POCO::Logger 是日志框架的主入口点。
#include “Poco/Logger.h”
|---- "" (the root logger)
|
|-----"HTTPServer"
|
|-----"HTTPServer.RequestHandler"
|
|-----"HTTPServer.RequestHandler.File"
|
|-----"HTTPServer.RequestHandler.CGI"
|
|------"HTTPServer.Listener"
//!."HTTPServer.RequestHandler.CGI" 继承"HTTPServer.RequestHandler"的级别和通道
访问日志对象
#include "Poco/Logger.h"
using Poco::Logger;
int main(int argc, char** argv)
{
Logger& logger = Logger::get("MyLogger");
logger.information("This is an informational message");
logger.warning("This is a warning message");
return 0;
}
通道属性
void setProperty(const std::string& name, const std::string& value)
std::string getProperty(const sdt::string& name)
例如文件通道(FileChannel),使用智能指针管理内存
//name属性 说明
//path 主文件路径
//secondaryPath 备用文件路径。默认为.1
void setProperty(const std::string& name, const std::string& value);
#include "Poco/FileChannel.h"
//实例化
AutoPtr模板类,类型为PatternFormatter
AutoPtr formatter(new PatternFormatter);
formatter->setProperty("times","local");
formatter->setProperty("pattern","%Y-%m-%d-%m-%d %H:%M:%s-%Y:%t");
file_channel>setProperty("path",switchFileName(logFilename));
控制台通道(ConsoleChannel)
AutoPtr console_channel(new ConsoleChannel);
分发通道(SplitterChannel)
void addChannel(Channel* pChannel)//添加一个新通道到Poco::SplitterChannel
#include "Poco/SplitterChannel.h"
AutoPtr<SplitterChannel> splitter_Channel(new SplitterChannel);
splitter_Channel->addChannel(file_channel);
splitter_Channel->addChannel(console_channel);
#include "Poco/LogStream.h"
#include "Poco/FormattingChannel.h"
#include "Poco/Formatter.h"
AutoPtr pattern_formatter (new PatternFormatter("%L%H:%M:%S-code line :%u-%U : %t"));
AutoPtr formatter_channle(new FormattingChannel(pattern_formatter , file_channel));
模式格式化类(PatternFormatter)