log4cplus使用资料手册 8

 
1 下载log4cpp并解压。

2 打开\log4cpp-0.3.4b\msvc6\msvc6.dsw
  编译log4cpp工程Release版。
  
3 将编译后的log4cpp.lib复制到VC的Lib目录中。

4 将头文件的目录log4cpp-0.3.4b\include\log4cpp\
  复制到VC的Include目录. 
  (或者添加log4cpp-0.3.4b\include到VC的Include环境变量)

6 目标工程包含库
log4cpp.lib ws2_32.lib
(要选择库连接方式相同的库)

5 包含头文件
日志记录
#include 
日志配置读取
#include 
NDC
#include 

9 日志代码
每个类可以有自己的类别(log4cpp::Category),
可以在配置文件中添加该类别并设置日志级别。
所有的log4cpp::Category都使用同一个Appender
不同的Category配置为不同的日志级别,就可以控制日志输出的范围。
一般只使用四个记录日志级:DEBUG,INFO,WARN,ERROR
如:
log4cpp::Category::getRoot().info("Now run line %d", __LINE__);
或使用非根类别
log4cpp::Category::getInstance("MyCat").info("Now run line %d", __LINE__);

使用流:
log4cpp::Category::getInstance("main_cat").infoStream()
    << "This will show up as " 
    << 1 << " emergency message" 
    << log4cpp::CategoryStream::ENDLINE;
 
具体的函数说明见api文档.

7 读取配置代码
读取log配置文件,应在log4cpp静态成员初始化之后。
如在CXXXApp::InitInstance()中

    try
    {
        log4cpp::PropertyConfigurator::configure("log.ini");
    }
    catch (log4cpp::ConfigureFailure e)
    {
        log4cpp::Category::getRoot().warn(e.what());
    }
    

8 配置文件

[log4cpp]
# Set root category priority to DEBUG and its only appender to A1.  
# priority enum: "FATAL", "ALERT", "CRIT", "ERROR", "WARN",
#            "NOTICE", "INFO", "DEBUG", "NOTSET", "UNKNOWN" 
rootCategory=DEBUG,A1
additivity.rootCategory=false
 
# define appender
appender.A1=RollingFileAppender
#appender.A1.threshold=NOTSET
appender.A1.fileName=XXXX.log
#appender.A1.maxFileSize=10485760
#appender.A1.maxBackupIndex=1
appender.A1.layout=PatternLayout
appender.A1.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S}](%p)%c %x: %m%n

appender.Info_Cons=ConsoleAppender
appender.Info_Cons.threshold=INFO
appender.Info_Cons.layout=PatternLayout
appender.Info_Cons.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S}](%p)%c %x: %m%n

# category for sub1 
category.sub1=DEBUG,A1,Info_Cons
additivity.sub1=false
category.sub2=INFO,A1
additivity.sub1=false

# other categories

[others]
djkf=dksajf
原文地址:https://www.cnblogs.com/rosesmall/p/2469552.html