ubuntu下安裝與使用Log4cpp
1、下載版本:log4cpp-1.1rc3.tar.gz, 該版本中沒有1.0版本的那些bug,不需修改,下載地址:http://sourceforge.net/projects/log4cpp/
2、解壓下載的壓縮文件:log4cpp-1.1rc3.tar.gz,可以使用命令行,或者右鍵壓縮包,在彈出菜單項中選擇“解壓至此處”
3、解壓後的文件夾名爲log4cpp,在終端(快捷鍵ctrl+alt+t)中,cd到該文件夾下,注意需要root權限。
4、運行configure
#./configure --with-pthreads
5、在終端中依次輸入如下命令,即可完成安裝,安裝完成後,log4cpp so庫在/usr/local/lib下,頭文件在/usr/local/include目錄下
#./configure
#make
#make install
6、添加環境變量
#gedit /etc/profile.d/log4cpp.sh
在文件中添加如下文字(注意不要少了冒號):
LD_LIBRARY_PATH=:$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH
7、保存好文件後,修改該文件可執行權限,在終端中輸入的命令如下:
#chmod a+x /etc/profile.d/log4cpp.sh
8、使用命令ldconfig -v後上述配置方可生效,在終端(ctrl+alt+t)中直接輸入ldconfig -v,注意需要root權限(在終端中輸入sudo su,然後回車輸入密碼),
desktop:/usr/local/lib# ldconfig -v
否則在程序運行後會出現如下找不到動態庫的問題:
./test_main: error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file or directory
9、編譯程序的指令爲:
g++ test_main.cpp -o test_main -llog4cpp -lpthread
其中:test_main.cpp爲上述源文件,test_main爲目標文件,-llog4cpp告訴編譯器使用了log4cpp的so庫,如果剛纔 log4cpp安裝時使用默認路徑,則該動態庫在/user/local/lib下,文件名爲liblog4cpp.so,-lpthread告訴編譯器 在liblog4cpp中使用了線程。
10、不使用配置文件的測試代碼(下面的代碼摘自網絡):
//test_main.cpp
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/BasicLayout.hh>
int main()
{
log4cpp::Layout* layout = new log4cpp::BasicLayout();
// 2. 初始化一個appender 對象
log4cpp::Appender* appender = new log4cpp::FileAppender("FileAppender","./test_log4cpp1.log");
// 3. 把layout對象附着在appender對象上
appender->setLayout(layout);
// 4. 實例化一個category對象
log4cpp::Category& warn_log = log4cpp::Category::getInstance("mywarn");
// 5. 設置additivity爲false,替換已有的appender
warn_log.setAdditivity(false);
// 5. 把appender對象附到category上
warn_log.setAppender(appender);
// 6. 設置category的優先級,低於此優先級的日誌不被記錄
warn_log.setPriority(log4cpp::Priority::WARN);
// 記錄一些日誌
warn_log.info("Program info which cannot be wirten");
warn_log.debug("This debug message will fail to write");
warn_log.alert("Alert info");
// 其他記錄日誌方式
warn_log.log(log4cpp::Priority::WARN, "This will be a logged warning");
log4cpp::Priority::PriorityLevel priority;
bool this_is_critical = true;
if(this_is_critical)
priority = log4cpp::Priority::CRIT;
else
priority = log4cpp::Priority::DEBUG;
warn_log.log(priority,"Importance depends on context");
warn_log.critStream() << "This will show up << as " << 1 << " critical message"<< log4cpp::Priority::ERROR;
// clean up and flush all appenders
log4cpp::Category::shutdown();
return 0;
}
11、使用配置文件,
(1)配置文件log4cpp.conf內容如下(摘自http://www.ibm.com/developerworks/cn/linux/l-log4cpp/index.html):
############################## log4cpp.conf ########################################################
# a simple test config
#定義了3個category sub1, sub2, sub1.sub2
log4j.rootCategory=DEBUG, rootAppender
log4j.category.sub1=,A1
log4j.category.sub2=INFO
log4j.category.sub1.sub2=ERROR, A2
# 設置sub1.sub2 的additivity屬性
log4j.additivity.sub1.sub2=false
#定義rootAppender類型和layout屬性
log4j.appender.rootAppender=org.apache.log4j.ConsoleAppender
log4j.appender.rootAppender.layout=org.apache.log4j.BasicLayout
#定義A1的屬性
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.fileName=A1.log
log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
#定義A2的屬性
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=The message '%m' at time %d%n
##################################################################################################
(2)測試代碼如下:
###############################test_usecfg.cpp#######################################################
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Category.hh>
int main() {
// 1 讀取解析配置文件
// 讀取出錯, 完全可以忽略,可以定義一個缺省策略或者使用系統缺省策略
// BasicLayout輸出所有優先級日誌到ConsoleAppender
try
{
log4cpp::PropertyConfigurator::configure("./log4cpp.conf");
}
catch (log4cpp::ConfigureFailure& f)
{
std::cout << "Configure Problem: " << f.what() << std::endl;
return -1;
}
//2 實例化category對象
// 這些對象即使配置文件沒有定義也可以使用,不過其屬性繼承其父category
// 通常使用引用可能不太方便,可以使用指針,以後做指針使用
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub2"));
log4cpp::Category& sub3 = log4cpp::Category::getInstance(std::string("sub3"));
log4cpp::Category& sub4 = log4cpp::Category::getInstance(std::string("sub4"));
// 正常使用這些category對象進行日誌處理。
root.fatal("root's log");
// sub1 has appender A1 and rootappender. since the additivity property is set true by default
sub1.info("sub1's log");
// sub2 has appender A2 appender. since the additivity property is set to false
sub2.alert("sub2's log");
// sub3 only has A3 appender. since the additivity property is set to false
sub3.debug("sub3's log");
sub3.alert("sub3's log");
// sub4 can not be found in the config file, so the root category's appender and layout are used
sub4.warn("sub4's log");
return 0;
}