zoukankan      html  css  js  c++  java
  • log4cxx安装使用

    log4cxx安装使用

    log4cxx现在是apache的一个项目,用来记录日志。看名字就知道,是给c++使用的。

    环境(在以下2个环境中进行验证测试):

    gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

    gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)


    log4cxx依赖于apache 的 apr 和 apr-util,所以安装顺序是: apr, apr-util, log4cxx。

    1.软件包下载
    apr: http://apr.apache.org/download.cgi
    apr-util: http://apr.apache.org/download.cgi
    log4cxx: http://logging.apache.org/log4cxx/download.html

    2.安装apr
    #tar xzvf apr-1.5.2.tar.bz2
    #cd apr-1.5.2
    #./configure --prefix=/usr/local/apr
    #make
    #make install

    3.安装apr-util
    #tar xzvf apr-util-1.5.4.tar.bz2
    #cd apr-util-1.5.4
    #./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    #make
    #make install

    4.安装log4cxx
    #tar xzf apache-log4cxx-0.10.0.tar.gz
    #cd apache-log4cxx-0.10.0
    log4cxx直接make会报类似error: ‘memmove’ was not declared in this scope的错误,参考前人的工作,修改以下源文件:
    src/main/cpp/inputstreamreader.cpp 添加 #include <string.h>
    src/main/cpp/socketoutputstream.cpp 添加 #include <string.h>
    src/examples/cpp/console.cpp 添加 #include <string.h> #include <stdio.h>
    #./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
    #make
    #make install

    5.把编译后的库加载到环境变量中(实际使用和发布使用log4cx库需要其他更多的工作)
    export LD_LIBRARY_PATH=/usr/local/apr/lib/:/usr/local/apr-util/lib/:/usr/local/log4cxx/lib/

    6.测试代码
    test.cpp

    #include <iostream>
    #include <log4cxx/logger.h>
    #include <log4cxx/propertyconfigurator.h>
    #include <log4cxx/helpers/exception.h>
    
    using namespace std;
    using namespace log4cxx;
    using namespace log4cxx::helpers;
    
    static const string CONF_LOG_FILE = "/home/fg/src/work/test/test.properties";
    
    LoggerPtr logger(Logger::getRootLogger());
    
    int main(void)
    {
            try {
                    PropertyConfigurator::configure(CONF_LOG_FILE);
                    LOG4CXX_INFO(logger, "Init() success.");
                    cout << "success" << endl;
            } catch (Exception &) {
                    cout << "log4cxx init error" << endl;
            }
            return 0;
    }
    test.cpp

    test.properties文件的内容:

    log4j.rootLogger=INFO, file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=log.dat
    log4j.appender.file.MaxFileSize=20MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    test.properties

    7.编译:
    g++ -o test0 test.cpp -L/usr/local/log4cxx/lib -llog4cxx -I/usr/local/log4cxx/include
    运行:
    ./test
    可以看到生成了log.dat文件,里面写入了LOG4CXX_INFO()输出的日志内容。

    参考资料:
    1.http://blog.chinaunix.net/uid-24512513-id-3195404.html
    2.http://zipperary.com/2015/08/04/log4cxx/

  • 相关阅读:
    [洛谷P3360]偷天换日
    [BZOJ3195]奇怪的道路
    [BAOJ3631]松鼠的新家
    [BZOJ4899]记忆的轮廓
    [BZOJ3940]Censoring
    P3303 [SDOI2013]淘金
    2019.8.5 NOIP模拟测试13 反思总结【已更新完毕】
    2019.8.3 NOIP模拟测试12 反思总结【P3938 斐波那契,P3939 数颜色,P3940 分组】
    2019.8.1 NOIP模拟测试11 反思总结
    洛谷P2178 [NOI2015]品酒大会
  • 原文地址:https://www.cnblogs.com/bugchecker/p/how_to_use_apache_log4cxx_logging_framework.html
Copyright © 2011-2022 走看看