log4cxx日誌庫RedHat下安裝

來源:互聯網
上載者:User

標籤:style   color   使用   檔案   2014   io   

今天領導交給我一個任務:把log4cxx庫在Redhat系統上面安裝起來

首先,我得到資訊,安裝這個庫一共需要三個軟體

apr-1.4.6.tar.gz

apr-util-1.4.1.tar.gz

apache-log4cxx-0.10.0.tar.gz

安裝順序是從上到下的,為什麼,因為後者的安裝依賴前者。

我整理了一下思路,有如下幾個問題:

1.安裝log4cxx庫到哪個目錄?

2.安裝好之後怎麼用?

我首先想到的是網上查資料,結果很多結果都顯示:

1.針對問題1,99%的都安裝在/usr/local目錄下(我的許可權是普通使用者,安裝在/usr/local目錄下需要root許可權,我頓時打了個寒戰,萬一我安裝錯了,伺服器暴了怎麼辦)

2.針對問題2,因為99%的都是安裝在/usr/local目錄下,結果就是隨便提供一個測試程式,直接來g++ test.cpp 其他簡單參數

我想了又想,最終按照第一步做了(在此之前我向領導要了管理員權限),安裝好後,沒什麼問題,不過,接下來,在進行寫程式測試的時候,關於log4cxx庫的檔案都找不到,我崩潰了,這和網上說的不一樣啊。

遇到了一些挫折,最終我是搞出來了,就來具體說說我的步驟吧。

1.把三個壓縮包放置到一個你喜歡的目錄下,比如/home/mac/log4cxx這個目錄下

2.在/home/mac/log4cxx目錄下解壓三個壓縮包得到三個檔案夾(解壓縮命令tar zxvf 你的目標)

3.在/home/mac/log4cxx目錄下建立三個檔案夾apr apr-util log4cxx作為三個包的安裝目錄(執行make install時的安裝目錄)

4.首先進入apr-1.4.6目錄執行./configure --prefix=/home/mac/log4cxx/apr配置apr包的安裝目錄為/home/mac/log4cxx/apr

5.執行make

6.執行make install

7.進入apr-util-1.4.1目錄執行./configure --prefix=/home/mac/log4cxx/apr-util --with-apr=/home/mac/log4cxx/apr意義和上面一步一樣,不過多了個依賴關係

8.執行make

9.執行make install

10.進入apache-log4cxx-0.10.0目錄執行./configure --prefix=/home/mac/log4cxx/log4cxx --with-apr=/home/mac/log4cxx/apr --with-apr-util=/home/mac/log4cxx/apr-util意義和上面一步一樣,依賴關係又體現了

11.執行make

12.執行make install

13.此時使用命令ls 瀏覽建立的三個檔案夾apr apr-util log4cxx,會發現裡面有一些include啊,lib啊之類的檔案夾

14.因為測試程式要用到log4cxx的庫檔案,所以還需要一步。執行vi /home/mac/.bashrc 在裡面添加兩句

LD_LIBRARY_PATH=/home/mac/log4cxx/log4cxx/lib

export LD_LIBRARY_PATH 然後儲存

15.執行source /home/mac/.bashrc使剛剛做的變動生效

16.編寫一個測試程式main.cpp

#include <log4cxx/logger.h>    #include <log4cxx/logstring.h> #include <log4cxx/propertyconfigurator.h>int main(int argc, char* argv[]) {         using namespace log4cxx;        // 讀取設定檔         PropertyConfigurator::configure("conf.log");        // 建立兩個logger         LoggerPtr logger1 = Logger::getLogger("TraceYourMama");         LoggerPtr logger2 = Logger::getLogger("Patch");        LOG4CXX_TRACE(logger1, "跟蹤");         LOG4CXX_WARN(logger1, "警告");         LOG4CXX_DEBUG(logger1, "調試");         LOG4CXX_ASSERT(logger1, false, "斷言");         LOG4CXX_FATAL(logger1, "致命");        LOG4CXX_TRACE(logger2, "跟蹤");         LOG4CXX_ERROR(logger2, "錯誤");         return 0;  }
17.編寫檔案conf.log這個log4cxx設定檔

log4j.rootLogger=TRACE, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=./ZW.loglog4j.appender.logfile.MaxFileSize=100KBlog4j.appender.logfile.MaxBackupIndex=10log4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d [%t] %-5p %c - %m% 
18.執行g++ -I/home/mac/log4cxx/log4cxx/include -L/home/mac/log4cxx/log4cxx/lib -llog4cxx main.cpp 

19.會產生a.out檔案

20.執行./a.out 終端輸出

2014-07-21 23:14:06,735 [0xb7fe4700] TRACE TraceYourMama - trace2014-07-21 23:14:06,736 [0xb7fe4700] WARN  TraceYourMama - warn2014-07-21 23:14:06,736 [0xb7fe4700] DEBUG TraceYourMama - debug2014-07-21 23:14:06,736 [0xb7fe4700] ERROR TraceYourMama - assert2014-07-21 23:14:06,736 [0xb7fe4700] FATAL TraceYourMama - fatal2014-07-21 23:14:06,736 [0xb7fe4700] TRACE Patch - trace2014-07-21 23:14:06,736 [0xb7fe4700] ERROR Patch - error
同時,目前的目錄下也會有尾碼名為log的檔案產生

再來從頭回答那兩個問題

1.隨你安裝在哪個目錄,是要gcc的時候指定好正確路徑就行了

2.還是gcc的 -I -L -i的參數運用值得深究

好了,到目前為止,終於讓這個庫起作用了,下一步就是分析如何配置這個庫。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.