log4cpp中可以在main()中设定root 型Category后,在其它文件中或线程中获得其子Category,从而反日志写入同一个文件。如
xxx.cpp中内容为
void Test::exam() { Category &sub=Category::getRoot().getInstance("sub"); sub.info("this is in exam"); }
在main.cpp中
void main()
{ RollingFileAppender *rollingfileappender=new RollingFileAppender("rollingfileappender","log"); PatternLayout *layout=new PatternLayout(); layout->setConversionPattern("%d:%p %c %x: %m%n"); rollingfileappender->setLayout(layout); Category & root=Category::getRoot(); root.addAppender(rollingfileappender); root.setPriority(Priority::INFO); }
这样main()中先获得root,然后可从中得到sub
在 编译的时候要加上 -llog4cpp -lpthread