Centos下安装Log4cxx

Log4cxx是开放源代码项目Apache Logging Service的子项目之一,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计。
要使用log4cxx,首先要从官网下载,官网是http://logging.apache.org/log4cxx/index.html,目前的最新版本是0.10.0,下载地址:http://www.apache.org/dyn/closer.cgi/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz。安装方式可以使用ant,也可以使用gcc编译安装,本文以gcc编译安装为主。安装log4cxx需要apr支持,apr官网地址是:http://apr.apache.org/,需要下载apr和apr_util两个组件。


注意配置前需进行以下操作:
1.vim src/main/cpp/inputstreamreader.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/inputstreamreader.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现inputstreamreader.cpp:66: error: 'memmove' was not declared in this scope
make[3]: *** [inputstreamreader.lo] 错误 1


2.vim src/main/cpp/socketoutputstream.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/socketoutputstream.h>
#include <log4cxx/helpers/socket.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现socketoutputstream.cpp:52: error: 'memcpy' was not declared in this scope



3.vim src/examples/cpp/console.cpp
增加#include <string.h>,#include <stdio.h>;
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <log4cxx/logger.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/logmanager.h>
#include <iostream>
#include <locale.h>
否则会出现
console.cpp: In function ‘int main(int, char**)’:
console.cpp:58: 错误:‘puts’在此作用域中尚未声明


安装步骤如下:
cd ~/libs
tar xjvf apr-1.4.4.tar.bz2
cd apr-1.4.4
./configure --prefix=${HOME}/libs && make && make install
cd ..
tar xjvf apr-util-1.3.11.tar.bz2
cd apr-util-1.3.11
./configure --prefix=${HOME}/libs --with-apr=${HOME}/libs && make && make install
cd ..
tar xzvf apache-log4cxx-0.10.0.tar.gz
cd apache-log4cxx-0.10.0
./configure --with-charset=utf-8 --with-apr=${HOME}/libs --with-apr-util=${HOME}/libs && make && make install
默认安装后会在/usr/local/include目录下包含log4cxx目录,该目录下就是log4cxx的相关头文件。




open ~/.bashrc 
add  LD_LIBRARY_LIB=$LD_LIBRARY_LIB:/usr/local/lib
add  LD_LIBRARY_LIB=$LD_LIBRARY_LIB:/home/username/libs/apr-util-1.5.1/xml/expat/.libs

username是你的登录名


下面写个例子(采用基本配置方式,即不使用配置文件)


    #include <log4cxx/logger.h>
    #include <log4cxx/basicconfigurator.h>
    #include <log4cxx/helpers/exception.h>


    using namespace log4cxx;
    using namespace log4cxx::helpers;


    LoggerPtr logger(Logger::getLogger("MyApp"));


    int main(int argc, char **argv)
    {
        int result = EXIT_SUCCESS;
        try
        {
            BasicConfigurator::configure();
            LOG4CXX_INFO(logger,"Entering application");
        }
        catch(Exception&)
        {
            result =EXIT_FAILURE;
        }
        return result;
    }


在我们用g++命令编译的时候,可能会出现链接错误,从晚上找了半天,原来编译的时候要用-l参数指定log4cxx命名空间。如下:


    g++ -llog4cxx -o myApp myApp.cpp


这样就会产生myApp可执行文件了。
在执行的时候,你可能会得到下面的错误:


    ./a.out: error while loading shared libraries: liblog4cxx.so.10: cannot open shared object file: No such file or directory


查找半天发现在/usr/local/lib下有liblog4cxx.so.10文件,这个问题是因为你没有设置LD_LIBRARY_PATH环境变量造成的。执行


    export LD_LIBRARY_PATH=/usr/local/lib


再运行,看到树下的输出了吧:


    0 [0xb7fb36c0] INFO MyApp null - Entering application


如果需要写配置文件,例子如下:


    #include "log4cxx/logger.h"
    #include "log4cxx/propertyconfigurator.h"


    static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("recommend_updater"));


    int main(int argc, char *argv[])
    {
            log4cxx::PropertyConfigurator::configure("./log4cxx_hello.properties");
            LOG4CXX_INFO(logger, "use properities file");
            return 0;
    }


log4cxx_hello.properties的内容如下:


    log4j.rootLogger=debug, R
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender


    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=./hello.log
    log4j.appender.R.MaxFileSize=100KB
    # Keep one backup file
    log4j.appender.R.MaxBackupIndex=10
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%5p %c [%t] (%F:%L) - %m%n


上面的配置了大小为100K的滚动日志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值