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

相关文章: