- categories 日志的层级体系
- appenders 日志打印到什么地方,可以是文件,也可以是终端
- layouts 日志输出格式,定义类似于printf中的输出方式。有三种,后面都有例子,这三种分别是:basic,simple,pattern
- priority 日志的级别,所有的级别有:EMERG、FATAL、ALERT、CRIT、ERROR、WARN、NOTICE、INFO、DEBUG、NOTSET,其中NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG。这个优先级的含义是:配置文件中设置了级别是debug,则任意的log都能打出来;如果配置了级别是fatal,则只有高于他优先级的日志才可以打印出来
- additivity 限制appender会不会被继承(后面还有补充说明)
2. 程序举例
#include<log4cpp/Category.hh>
#include<log4cpp/FileAppender.hh>
#include<log4cpp/SimpleLayout.hh>#defineLOGFILE"./test.log"intmain(){
/*SettingupAppender,layoutandCategory*/
log4cpp::Appender*appender=newlog4cpp::FileAppender("FileAppender",LOGFILE);//第一个参数是appender的名字,第二个是log文件的名字
log4cpp::Layout*layout=newlog4cpp::SimpleLayout();
//log4cpp::Layout*layout=newlog4cpp::BasicLayout();log4cpp::Category&category=log4cpp::Category::getInstance("abc");
appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);
/*Theactuallogging*/
category.info("Thisisfortracingtheflow");
category.notice("Thisistonotifycertainevents");
category.warn("Thisistogeneratecertainwarnings");
}
以上程序将得到下面的log输出:
INFO : This is for tracing the flow
NOTICE : This is to notify certain events
WARN : This is to generate certain warnings
这个程序里面可以把log4cpp::Layout*layout=newlog4cpp::BasicLayout();的注释取消,注释掉上面的语句。将得到下面的log输出:
1308806376 WARN abc : This is to generate certain warnings
两者的区别就在于使用了不同的layout,一个是simple一个是basic的。
要做到复杂格式的输出就要使用第三种,即:PatternLayout,下面借助配置文件来说明这种方式的使用:
3. 配置文件说明
配置文件log4cpp.conf:
#其中sub2和sub3设置了additivity属性为false;sub1的additivity属性默认为true
rootCategory=DEBUG,rootAppender
category.sub1=,A1
category.sub2=INFO,A2
additivity.sub2=false
category.sub3=ERROR,A3
additivity.sub3=false
#定义rootAppender类型和layout属性,这里使用了BasicLayout
appender.rootAppender=org.apache.log4cpp.ConsoleAppender
appender.rootAppender.layout=org.apache.log4cpp.
#定义A1的属性,这里使用了SimpleLayout
appender.A1=org.apache.log4cpp.FileAppender
appender.A1.fileName=./log/A1.log
appender.A1.layout=org.apache.log4cpp.SimpleLayout
#定义A2的属性,这里使用了PatternLayout
appender.A2=org.apache.log4cpp.ConsoleAppender
appender.A2.layout=org.apache.log4cpp.PatternLayout
appender.A2.layout.ConversionPattern=Themessage'%m'attime%d%n
#定义A3的属性
appender.A3=org.apache.log4cpp.RollingFileAppender
appender.A3.fileName=./log/A3.log
appender.A3.maxFileSize=50
appender.A3.maxBackupIndex=3
appender.A3.backupPattern=%Y-%m-%d
appender.A3.layout=org.apache.log4cpp.PatternLayout
appender.A3.layout.ConversionPattern=%d{%Y-%m-%d%H:%M:%S}[%p]:[%c]%m%n
4. 带配置文件的程序举例
源代码:
#include"log4cpp/PropertyConfigurator.hh"intmain(intargc,char*argv[]){
//1读取解析配置文件
//读取出错,完全可以忽略,可以定义一个缺省策略或者使用系统缺省策略
//BasicLayout输出所有优先级日志到ConsoleAppendertry
{
log4cpp::PropertyConfigurator::configure("./log4cpp.conf");
}
catch(log4cpp::ConfigureFailure&f)
{
std::cout<<"ConfigureProblem"<<f.what()<<std::endl;
return-1;
}
//2实例化category对象
//这些对象即使配置文件没有定义也可以使用,不过其属性继承其父category
//通常使用引用可能不太方便,可以使用指针,以后做指针使用log4cpp::Category&root=log4cpp::Category::getRoot();
log4cpp::Category&sub1=log4cpp::Category::getInstance(std::string("sub1"));
log4cpp::Category&sub2=log4cpp::Category::getInstance(std::string("sub2"));
log4cpp::Category&sub3=log4cpp::Category::getInstance(std::string("sub3"));
log4cpp::Category&sub4=log4cpp::Category::getInstance(std::string("sub4"));
//正常使用这些category对象进行日志处理。root.fatal("root'slog");
//sub1hasappenderA1androotappender.sincetheadditivitypropertyissettruebydefaultsub1.info("sub1'slog");
//sub2hasappenderA2appender.sincetheadditivitypropertyissettofalsesub2.alert("sub2'slog");
//sub3onlyhasA3appender.sincetheadditivitypropertyissettofalsesub3.debug("sub3'slog");
sub3.alert("sub3'slog");
//sub4cannotbefoundintheconfigfile,sotherootcategory'sappenderandlayoutareusedsub4.warn("sub4'slog");
return0;
}
终端打印出来的log:
1308829427 WARN sub4 : sub4's log
INFO : sub1's log
log/A3.log的内容:
2011-06-23 19:27:50 [ALERT]: [sub3] sub3's log
5. 配置参考
Loggers have their additivity flag set to true by default.
参考来自:http://logging.apache.org/log4j/1.2/manual.html
Sets the format of log lines handled by thisPatternLayout.
By default, set to "%m%n".
Format characters are as follows:
Date format: The date format character may be followed by a date format specifier enclosed between braces. For example, %d{%H:%M:%S,%l} or %d{%d %m %Y %H:%M:%S,%l}. If no date format specifier is given then the following format is used: "Wed Jan 02 02:03:55 1980". The date format specifier admits the same syntax as the ANSI C function strftime, with 1 addition. The addition is the specifier %l for milliseconds, padded with zeros to make 3 digits.
参考来自:http://log4cpp.sourceforge.net/api/classlog4cpp_1_1PatternLayout.html
这也是一篇不错的参考文章: