标签:
(1)下载log4cpp的工程,编译生成lib文件和dll库
http://sourceforge.net/projects/log4cpp/files/
(2)添加头文件
#include "log4cpp/Category.hh"
#include "log4cpp/CategoryStream.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
(3)初始化代码
// 1实例化一个layout 对象
log4cpp::Layout* layout = new log4cpp::BasicLayout();
// 2. 初始化一个appender 对象
log4cpp::FileAppender* appender = new log4cpp::FileAppender("FileAppender", path);
// 3. 把layout对象附着在appender对象上
appender->setLayout(layout);
// 4. 实例化一个category对象
log4cpp::Category& logcat = log4cpp::Category::getInstance(CATRGORY_NAME);
// 5. 设置additivity为false,替换已有的appender
logcat.setAdditivity(false);
// 5. 把appender对象附到category上
logcat.setAppender(appender);
// 6. 设置category的优先级,低于此优先级的日志不被记录
switch (level)
{
case DebugLevel:
logcat.setPriority(log4cpp::Priority::DEBUG);
break;
case InfoLevel:
logcat.setPriority(log4cpp::Priority::INFO);
break;
case WarnLevel:
logcat.setPriority(log4cpp::Priority::WARN);
break;
case ErrLevel:
logcat.setPriority(log4cpp::Priority::ERROR);
break;
default:
logcat.setPriority(log4cpp::Priority::INFO);
break;
}
(4)写日志代码
logcat.info("Program info which cannot be wirten");
logcat.debug("This debug message will fail to write");
logcat.alert("Alert info");
标签:
原文地址:http://www.cnblogs.com/monkeyfeng/p/4631348.html