- 指定输出地点,并指定样式Layout
- 获取Category 的根,并且添加Appender
- 设置Category的优先级
- 记录日志
- 关闭Category
注意头文件是后缀 .hh
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>
using namespace std;int main(int argc, char* argv[])
{
log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender",&cout);
osAppender->setLayout(new log4cpp::BasicLayout()); log4cpp::Category& root = log4cpp::Category::getRoot(); root.addAppender(osAppender);
root.setPriority(log4cpp::Priority::DEBUG);
root.error("Hello log4cpp in a Error Message!");
root.warn("Hello log4cpp in a Warning Message!");
log4cpp::Category::shutdown();
return 0;
}
编译:g++ helloworld.cpp -o helloworld -llog4cpp -lpthread