log4cxx安裝和使用


 log4cxx是Java社區著名的log4j的c++移植版,用於為C++程序提供日志功能,以便開發者對目標程序進行調試和審計,log4cxx是apache軟件基金會的開源項目,基於APR實現跨平台支持。一個良好的日志系統不管是開發、調試和維護,對一個項目來說是多么的重要,類似的日志框架還有GLog、boost log。

依賴:apr、apr-util
apr、apr-util下載地址: http://apr.apache.org/download.cgi

二、下載與安裝

環境:linux
依賴:apr、apr-util
apr、apr-util下載地址: http://apr.apache.org/download.cgi
 
安裝步驟:
1.由於log4cxx是依賴於apr的,因此先安裝apr和apr-util這兩個庫,我會將這兩個庫安裝到同一個目錄下;
下載完之后,當前目錄有 apr-1.4.6.tar.gzapr-util-1.5.1.tar.gz兩個文件。
[plain]  view plain copy print ?
 
  1. $>tar xvf apr-1.4.6.tar.gz  
  2. $>cd apr-1.4.6  
  3. $>./configuer --prefix=/usr/local/apr  
  4. $>make  
  5. $>make install  


目前apr安裝完成,會在/usr/local下產生apr目錄
[plain]  view plain copy print ?
 
  1. $>tar xvf apr-util-1.5.1.tar.gz  
  2. $>cd apr-util-1.5.1  
  3. $>./configure  --prefix=/usr/local/apr --with-apr=/usr/local/apr  
  4. $>make  
  5. $>make install  


apr-util也會安裝/usr/local/apr目錄下
 
2.開始安裝log4cxx,下載完之后,當前目錄下有apache-log4cxx-0.10.0.tar.gz。
[plain]  view plain copy print ?
 
  1. $>tar xvf apache-log4cxx-0.10.0.tar.gz  
  2. $>cd apache-log4cxx-0.10.0  
  3. $>./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr  --with-charset=utf-8 --with-logchar=utf-8  
  4. $>make  


注意:由於我用的是ubuntu系統,默認編碼是utf-8,因此必須要加上 --with-charset=utf-8 --with-logchar=utf-8,不然中文輸出是一堆亂碼。

這時候會出現這樣的錯誤
[plain]  view plain copy print ?
 
  1. inputstreamreader.cpp:66: error: 'memmove' was not declared in this scope  
  2. make[3]: *** [inputstreamreader.lo] 錯誤 1  

在網上找了相關資料,原來有幾個.cpp文件缺少了一些引用頭文件,添加上去即可。
 
  1. src/main/cpp/inputstreamreader.cpp添加#include <string.h>  
  2. src/main/cpp/socketoutputstream.cpp添加#include <string.h>  
  3. src/examples/cpp/console.cpp添加#include <string.h>;#include <stdio.h>;  

這些文件修改完之后,執行make和make install就可安裝成功。
 

三、測試

主函數代碼:main.cpp
 
  1. #include <log4cxx/logger.h>  
  2. #include <log4cxx/basicconfigurator.h>  
  3. #include <log4cxx/propertyconfigurator.h>  
  4. #include <log4cxx/helpers/exception.h>  
  5. #include <iostream>  
  6. int main()  
  7. {  
  8. log4cxx::PropertyConfigurator::configureAndWatch("log4cxx.properties");  
  9. log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("lib"));  
  10. LOG4_DEBUG(logger, "this is log4cxx test");  
  11. return 0;  
  12. }  

配置文件:log4cxx.properties
 
 
  1. # 設置root logger為DEBUG級別,使用了ca和fa兩個Appender  
  2. log4j.rootLogger=DEBUG,lib  
  3.   
  4. #對Appender lib進行設置:  
  5. # 這是一個文件類型的Appender,  
  6. # 其輸出文件(File)為./lib.log,  
  7. # 輸出方式(Append)為覆蓋方式,  
  8. # 輸出格式(layout)為PatternLayout  
  9. log4j.appender.lib=org.apache.log4j.ConsoleAppender  
  10. log4j.appender.lib.Threshold=DEBUG  
  11. #log4j.appender.lib.DatePattern='log/'yyyy-MM-dd'_SysService.log'  
  12. log4j.appender.lib.File=./log/output.log  
  13. log4j.appender.lib.Append=true  
  14. log4j.appender.lib.layout=org.apache.log4j.PatternLayout  
  15. log4j.appender.lib.layout.ConversionPattern=[%-5p] %d %l : %m%n  
[plain]  view plain copy print ?
 
  1. $>g++ -o test -I/usr/local/apr/include -L/usr/local/apr/lib -lapr-1 -laprutil-1 main.cpp  
  2. $./test  
會在屏幕中打印出
[DEBUG] 2012年11月14日 15:16:17,890 main.cpp(10) : test
先寫到這里,下篇將會介紹log4cxx的配置。
 
 
我使用報錯,沒找到文件:

defines.h:24:28: fatal error: log4cxx/logger.h: No such file or directory
#include <log4cxx/logger.h>

我是裝在:/usr/local/log4cxx 目錄

下面有2個目錄,include和lib。

/usr/local/log4cxx/include/log4cxx 才是頭文件。

我們在gcc加上-I/usr/local/log4cxx/include 即可。

加上lib目錄:-L/usr/local/log4cxx/lib

使用時

//usr/local/apr/lib/libapr-1.so.0: undefined reference to `uuid_generate@UUID_1.0'

 

加上

 

 

參考:

 http://blog.csdn.net/fhxpp_27/article/details/8280024

http://www.linuxidc.com/Linux/2012-07/66663.htm

 


免責聲明!

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



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