ubuntu下log4cxx安裝使用


需要安裝log4cxx,安裝的過程中可是充滿了坎坷。。。最大的問題是在make log4cxx時,總是報undefined XML什么什么的錯誤,查了一下也沒解決了,然后把apr-utils刪了重新裝了一下就好了。。

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


復制代碼
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
復制代碼

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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM