サンプル12 testProperties.cpp

 log4cppの設定ファイルがどのように解析されるかを知ることができるサンプルです。

関連ファイル
  • testProperties.cpp
  • log4cpp.properties
ファイルの中身

testProperties.cpp

  1. #include "Properties.hh"
  2. #include <iostream>
  3. #include <fstream>
  4. int main(int argc, char** argv) {
  5. log4cpp::Properties properties;
  6. std::string inFile = (argc > 1) ? argv[1] : "log4cpp.properties";
  7. std::ifstream in(inFile.c_str());
  8. properties.load(in);
  9. properties.save(std::cout);
  10. }

log4cpp.properties

# Set root category priority to DEBUG and its only appender to A1.
log4j.rootCategory=ERROR, A4

# categories for Board Control
log4j.category.BoardControl=INFO, A4
log4j.category.BoardControl.additivity=false

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.BasicLayout

log4j.appender.A2=org.apache.log4j.RemoteSyslogAppender
log4j.appender.A2.syslogName=log4cppApp
log4j.appender.A2.syslogHost=localhost
log4j.appender.A2.facility=-1
log4j.appender.A2.portNumber=-1
log4j.appender.A2.layout=org.apache.log4j.BasicLayout

log4cpp.appender.A3=org.apache.log4j.Win32DebugAppender
log4cpp.appender.A3.layout=org.apache.log4j.BasicLayout

appender.A4=org.apache.log4j.FileAppender
appender.A4.fileName=smas2.log
appender.A4.layout=org.apache.log4j.BasicLayout

実行結果例

 testProperties.cppをビルドし、実行した結果です。実行環境はLinux(Ubuntu 11.0)です。

stdout(標準出力):

priority debug(700): DEBUG
priority debug(700): 700
priority debug(700): 700
caught unknown priority name: '700arghh'
takashi@takashi-virtual-machine:/mnt/hgfs/share/log4cpp/tests$ ./testProperties
appender.A1=org.apache.log4j.ConsoleAppender
appender.A1.layout=org.apache.log4j.BasicLayout
appender.A2=org.apache.log4j.RemoteSyslogAppender
appender.A2.facility=-1
appender.A2.layout=org.apache.log4j.BasicLayout
appender.A2.portNumber=-1
appender.A2.syslogHost=localhost
appender.A2.syslogName=log4cppApp
appender.A3=org.apache.log4j.Win32DebugAppender
appender.A3.layout=org.apache.log4j.BasicLayout
appender.A4=org.apache.log4j.FileAppender
appender.A4.fileName=smas2.log
appender.A4.layout=org.apache.log4j.BasicLayout
category.BoardControl=INFO, A4
category.BoardControl.additivity=false
rootCategory=ERROR, A4