1 配置文件
log4cpp.rootCategory=DEBUG, rootAppender
log4cpp.category.sub1=DEBUG, A1, A2
log4cpp.category.sub1.sub2=DEBUG, A3
log4cpp.category.txhAppender=DEBUG, txh
log4cpp.appender.rootAppender=ConsoleAppender
log4cpp.appender.rootAppender.layout=PatternLayout
log4cpp.appender.rootAppender.layout.ConversionPattern=%d [%p] %m%n
log4cpp.appender.A1=FileAppender
log4cpp.appender.A1.fileName=A1.log
log4cpp.appender.A1.layout=BasicLayout
log4cpp.appender.A2=FileAppender
log4cpp.appender.A2.threshold=WARN
log4cpp.appender.A2.fileName=A2.log
log4cpp.appender.A2.layout=PatternLayout
log4cpp.appender.A2.layout.ConversionPattern=%d [%p] %m%n
log4cpp.appender.A3=RollingFileAppender
log4cpp.appender.A3.fileName=A3.log
log4cpp.appender.A3.maxFileSize=200
log4cpp.appender.A3.maxBackupIndex=1
log4cpp.appender.A3.layout=PatternLayout
log4cpp.appender.A3.layout.ConversionPattern=%d [%p] %m%n
log4cpp.appender.txh=SyslogAppender
log4cpp.appender.txh.layout=PatternLayout
log4cpp.appender.txh.layout.ConversionPattern=%d [%p] %m%n
2 代码
#include <unistd.h>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include "log4cpp/SyslogAppender.hh"
#include "log4cpp/LoggingEvent.hh"
#include <log4cpp/PatternLayout.hh>
int main(int argc , char* argv[])
{
try
{
log4cpp::PropertyConfigurator::configure("./log4cpp.conf");
}
catch(log4cpp::ConfigureFailure& f)
{
std::cerr << f.what() << std::endl;
return -1;
}
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
log4cpp::Category& txh = log4cpp::Category::getInstance(std::string("txhAppender"));
root.warn("stom is coming");
sub1.debug("Received storm warning");
sub1.info("closing all hatches");
sub2.debug("Hiding solar panels");
sub2.error("Solar panels are blocked");
sub2.debug("Applying protective shield");
sub2.warn("Unfolding protective shield");
sub2.info("Solar panels are shielded");
sub1.info("All hatches closed");
root.info("Ready for storm.");
while(1)
{
txh.info("caonima");
sleep(1);
}
log4cpp::Category::shutdown();
}
3 编译
g++ test2.cpp -o main -llog4cpp -lpthread
|