log4cpp简单使用及踩到的坑

log4cpp是log4j的一个扩展, C++开发者可用该库记录日志,可输出到终端,亦可保存到文件。

下面简单demo展示如何输出日志到输出终端。

1 #include <iostream> 2 #include <log4cpp/Category.hh> 3 #include <log4cpp/OstreamAppender.hh> 4 #include <log4cpp/Priority.hh> 5 #include <log4cpp/PatternLayout.hh> 6 using namespace std; 7 8 int main(int argc, char const *argv[]) 9 { 10 log4cpp::OstreamAppender app("osAppender", &cout); 11 12 log4cpp::PatternLayout *layout = new log4cpp::PatternLayout(); 13 layout->setConversionPattern("%d: %p %c : %m%n"); 14 app.setLayout(layout); 15 16 log4cpp::Category &root = log4cpp::Category::getRoot(); 17 log4cpp::Category &infoCategory = root.getInstance(string(argv[0])); 18 19 infoCategory.addAppender(app); 20 infoCategory.setPriority(log4cpp::Priority::INFO); 21 22 infoCategory.info("system is running..."); 23 infoCategory.warn("system has got a warn..."); 24 infoCategory.error("system has got an error..."); 25 infoCategory.fatal("system has crashed...."); 26 27 log4cpp::Category::shutdown(); 28 return 0; 29 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywypy.html