1int main(int argc, char** argv) {
2 log4cpp::Appender* appender;
3#ifdef LOG4CPP_HAVE_SYSLOG
4 log4cpp::SyslogAppender* syslogAppender;
5
6 syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
7#else
8 log4cpp::Appender* syslogAppender;
9
10 syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
11#endif
12
13 if (argc < 2) {
14 appender = new log4cpp::OstreamAppender("default", &std::cout);
15 } else {
16 appender = new log4cpp::FileAppender("default", argv[1]);
17 }
18
19 syslogAppender->setLayout(new log4cpp::BasicLayout());
20 appender->setLayout(new log4cpp::BasicLayout());
21 //(Root Category name为空,默认为所有category的父类)
22 log4cpp::Category& root = log4cpp::Category::getRoot();
23 root.addAppender(syslogAppender);
24 root.setPriority(log4cpp::Priority::ERROR);
25
26 // //sub1的父类是root
27 log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
28 sub1.addAppender(appender);
29
30 //sub2的父类是sub1
31 log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
32
33 log4cpp::NDC::push(std::string("ndc1"));
34
35 //root设置了error级别,其他级别默认为800,未设置
36 std::cout << " root prio = " << root.getPriority() << std::endl;
37 std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
38 std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
39
40 root.error("root error"); //输出
41 root.warn("root warn"); //不输出
42
43 //因为sub1未设置级别,所以采取的是父类设置的error级别,category属性_isAdditive默认为true,所以sub1打印一遍之后,root category还会打印一次(parent category都会打印一遍)
44 sub1.error("sub1 error");
45 sub1.warn("sub1 warn"); //不输出
46
47 sub2.error("sub2 error"); //同sub1,最后调用的是sub1的callAppender函数,sub1 appender打印一次, root appender打印一次
48 sub2.warn("sub2 warn");
49
50 sub1.setPriority(log4cpp::Priority::INFO); //设置了sub1的打印级别
51 std::cout << " root prio = " << root.getPriority() << std::endl;
52 std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
53 std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
54
55 std::cout << "priority info" << std::endl;
56
57 root.error("root error");
58 root.warn("root warn");
59
60 sub1.error("sub1 error");
61 sub1.warn("sub1 warn");
62 sub2.error("sub2 error");
63 sub2.warn("sub2 warn");
64 sub2.error("sub2 :%d", 10);
65
66 std::string a("1234");
67 sub2.error(a);
68
69 sub2.warnStream() << "streamed warn";
70
71 sub2 << log4cpp::Priority::WARN << "warn2" << " warn3"
72 << log4cpp::eol << " warn4";
73
74 //因为前面设置了一次NDC1,所以下面每一条打印的NDC信息为:ndc i=xx
75 {
76 for(int i = 0; i < 10000; i++) {
77 char ndc2[20];
78 sprintf(ndc2, "i=%d", i);
79 log4cpp::NDC::push(ndc2);
80 sub1.info("%s%d", "i = ", i);
81 if ((i % 10) == 0) {
82 sub1.log(log4cpp::Priority::NOTICE, "reopen log");
83 if (log4cpp::Appender::reopenAll()) {
84 sub1.info("log reopened");
85 } else {
86 sub1.warn("could not reopen log");
87 }
88 }
89#ifndef WIN32
90 sleep(1);
91#endif
92 log4cpp::NDC::pop();
93 }
94 }
95
96 return 0;
97}
98 sub1 设置为debug, sub2为error, sub1是sub2的父类,sub2 _isAdditive= true
sub2.error(111); 这句sub1仍然会打印(判断是否打印是sub2的级别比较的)
log4cpp::Category::getInstance如果配置中不存在的category,那么会新创建一个category,并且以(name为空"")的category作为parent category。
posted on 2014-06-18 12:39
pizzx 阅读(862)
评论(0) 编辑 收藏 引用 所属分类:
c++/boost