2009年04月10日

log4cxx

JavaではロギングにLog4Jを使用している。

現在、Linux上でC++を使用してアプリを書いているが、同じようにリッチな情報でロギングしたい。
と言うことで、同じapacheのLog4cxxを使用する。Log4cppではファイルの行番号などを一緒にログに出すことができないっぽいのでLog4cxxに。

Log4cxxセットアップメモ。

Log4cxxから落とす。
./configure --prefix=hoga
すると次のエラーが。APRが無いと怒られる。
checking for APR... no
configure: error: APR could not be located. Please use the --with-apr option.

仕方ないので入れる。
wget http://www.meisei-u.ac.jp/mirror/apache/dist/apr/apr-1.3.3.tar.gz

./configure --prefix=hoga
make
make install

そしてlog4cxxのconfigure。するとまた怒られる。今度はapr-utilが無いと言われる。
checking for APR-util... configure: error: --with-apr-util requires a directory or file to be provided

仕方ないので入れる。
wget http://www.meisei-u.ac.jp/mirror/apache/dist/apr/apr-util-1.3.4.tar.gz

./configure --prefix=hoga --with-apr=hoga
make && make install
Libraries have been installed in:
hoga

入ったようだ。

今度こそLog4cxxをconfigure。
./configure --prefix=hoga --with-charset=utf-8 --with-logchar=utf-8
make && make install

入った。


では使う。
export LD_LIBRARY_PATH=hoga
LD_LIBRARY_PATHを設定しないと以下のエラーメッセージ。
./a.out: error while loading shared libraries: liblog4cxx.so.10: cannot\
open shared object file: No such file or directory

コンパイル時にもちゃんと教えてあげる。
g++ -I/hoga/include/ -L/hoga/lib -llog4cxx test.cpp

#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>

using namespace log4cxx;

LoggerPtr logger = Logger::getLogger("TestCategory" );

int main( int argc , char** args ){
 PropertyConfigurator::configure("test.properties");
 LOG4CXX_DEBUG( logger , "test " << 10 <<"px");
 return 0;
}


以下読み込む設定ファイル。元はLog4cxxと小物ツールから拾ってきた。
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1} %l - %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=test.log
log4j.appender.file.Apppend=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1} %l - %m%n

log4j.rootLogger=debug, stdout, file
log4j.debug=false

結果は次のような感じ。
2009-04-10 15:43:15,457 DEBUG TestCategory test.cpp(12) - test 10px

Log4cxxのconfigure時に--with-charset=utf-8 --with-logchar=utf-8を付けないと日本語が文字化けする。

Comment on "log4cxx"

"log4cxx"へのコメントはまだありません。

Post a Comment

コメントする

コメント登録機能が設定されていますが、TypeKey トークンが設定されていません。