【学習ノート】log 4 cxxのインストールと使用

3870 ワード

一、紹介
log4cxx Java     log4j c++   ,   C++        ,                 ,log4cxx apache          ,  APR       。              、     ,             ,             。           ,                 ,         GLog、boost log,            。
      ,9c pass9          ,         log4cxx     ,             ,      c       ,             ,            ,      ,           ,   ,      ,          ,   、  、   、   、  、       。

二、ダウンロードとインストール
環境:linux
依存:apr、apr-util
apr、apr-utilダウンロードアドレス:http://apr.apache.org/download.cgi
log 4 cxxダウンロードアドレス:http://logging.apache.org/log4cxx/download.html
インストール手順:
1.log 4 cxxはaprに依存するので、まずaprとapr-utilの2つのライブラリをインストールして、私はこの2つのライブラリを同じディレクトリの下にインストールします.
ダウンロードが完了すると、現在のディレクトリはapr-1.4です.6.tar.gzとapr-util-1.5.1.tar.gzの2つのファイル.
$>tar xvf apr-1.4.6.tar.gz
$>cd apr-1.4.6
$>./configuer --prefix=/usr/local/apr
$>make
$>make install

現在aprのインストールが完了し、/usr/localでaprディレクトリが生成されます
$>tar xvf apr-util-1.5.1.tar.gz
$>cd apr-util-1.5.1
$>./configuer --prefix=/usr/local/apr --with-apr=/usr/local/apr
$>make
$>make install

apr-utilも/usr/local/aprディレクトリの下にインストールされます
2.log 4 cxxのインストールを開始し、ダウンロードが完了すると、現在のディレクトリの下にapache-log 4 cxx-0.10があります.0.tar.gz.
$>tar xvf apache-log4cxx-0.10.0.tar.gz
$>cd apache-log4cxx-0.10.0
$>./configuer --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr  --with-charset=utf-8 --with-logchar=utf-8
$>make

注意:ubuntuシステムを使用しているため、デフォルトの符号化はutf-8なので、--with-charset=utf-8--with-logchar=utf-8を追加しなければなりません.そうしないと、中国語の出力は文字化けになります.
こんな間違いが起こります
inputstreamreader.cpp:66: error: 'memmove' was not declared in this scope
make[3]: *** [inputstreamreader.lo]    1

ネットで関連資料を探したが、いくつかあった.cppファイルには参照ヘッダファイルがいくつか欠けているので、追加すればいいです.
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>;

これらのファイルの変更が完了したら、makeとmake installを実行してインストールに成功します.
三、テスト
主関数コード:main.cpp
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <iostream>
int main()
{
log4cxx::PropertyConfigurator::configureAndWatch("log4cxx.properties");
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("lib"));
LOG4_DEBUG(logger, "this is log4cxx test");
return 0;
}

プロファイル:log 4 cxx.properties
#   root logger DEBUG  ,   ca fa  Appender
log4j.rootLogger=DEBUG,lib

# Appender lib    :
#          Appender,
#      (File) ./lib.log,
#     (Append)     ,
#     (layout) PatternLayout
log4j.appender.lib=org.apache.log4j.ConsoleAppender
log4j.appender.lib.Threshold=DEBUG
#log4j.appender.lib.DatePattern='log/'yyyy-MM-dd'_SysService.log'
log4j.appender.lib.File=./log/output.log
log4j.appender.lib.Append=true
log4j.appender.lib.layout=org.apache.log4j.PatternLayout
log4j.appender.lib.layout.ConversionPattern=[%-5p] %d %l : %m%n
$>g++ -o test -I/usr/local/apr/include -L/usr/local/apr/lib -lapr-1 -laprutil-1 main.cpp
$./test

画面に印刷されます
[DEBUG]2012年11月14日15:16:17890 main.cpp(10) : test
ここまで書いておきますが、log 4 cxxの構成について説明します.