Log4cpp
有一个总可用并实例化好的Category,即根Category。使用log4cpp::Category::getRoot()可以得到根
Category。在大多数情况下,一个应用程序只需要一个日志种类(Category),但是有时也会用到多个Category,此时可以使用根
Category的getInstance方法来得到子Category。不同的子Category用于不同的场合。一个简单的例子
CategoryExam如下所示:
#include <iostream>
#include<log4cpp/Category.hh>
#include<log4cpp/OstreamAppender.hh>
#include<log4cpp/BasicLayout.hh>
#include<log4cpp/Priority.hh>
using namespace std;
using namespace log4cpp;
int main()
{
OstreamAppender * osAppender1=new OstreamAppender("osAppender1",&cout);
osAppender1->setLayout(new BasicLayout());
OstreamAppender * osAppender2=new OstreamAppender("osAppender2",&cout);
osAppender2->setLayout(new BasicLayout());
Category & root=Category::getRoot();
//root.setPriority(Priority::DEBUG);
Category & sub1=root.getInstance("sub1");
sub1.addAppender(osAppender1);
sub1.setPriority(Priority::DEBUG);
sub1.error("sub error");
Category & sub2=root.getInstance("sub2");
sub2.addAppender(osAppender2);
sub2.setPriority(101);
sub2.warn("sub2 warning");
sub2.fatal("sub2 fatal");
sub2.alert("sub2 alert");
sub2.crit("sub2 crit"); // Priority=200
Category::shutdown();
return 0;
}
|