1. 主页:http://log4cpp.sourceforge.net
“Log4cpp is library of C++ classes for flexible logging to files, syslog, IDSA and other destinations. It is modeled after the Log4j Java library, staying as close to their API as is reasonable.”

api文档地址:http://log4cpp.sourceforge.net/api/hierarchy.html

 

2.下载安装
环境:ubuntu 14.04 LTS,gcc 4.8.4

下载源码包 log4cpp-1.1.2rc1.tar.gz
$tar xzvf log4cpp-1.1.2rc1.tar.gz
$cd log4cpp
$./configure --with-pthreads
$make
$make check
$make install

安装完成后,头文件在/usr/local/include/log4cpp,库安装在/usr/local/lib/,文件名liglog4cpp.so.5.0.6,还有几个符号链接

 

3.测试
测试代码 main.cpp

#include "log4cpp/Category.hh"
#include "log4cpp/PropertyConfigurator.hh"

int main(int argc, char* argv[])
{
    std::string initFileName = "log4cpp.properties";
    log4cpp::PropertyConfigurator::configure(initFileName);

    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"));

    root.warn("Storm 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.");

    log4cpp::Category::shutdown();

    return 0;
}
main.cpp

相关文章:

  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
相关资源
相似解决方案