目次
MyApp 例その2 (1 of 4)

// log4j クラスをインポートする
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;

public class MyApp {

  // "MyApp"という名前のCategoryインスタンスを
  // 参照するstatic category変数を定義する。
  static Category cat = Category.getInstance(MyApp.class);

  public static void main(String[] args) {

    // ログをコンソールに出力する簡単な設定をする
    PropertyConfigurator.configure(args[0]);

    cat.info("Entering application.");
    Bar bar = new Bar();
    bar.doIt();
    cat.info("Exiting application.");
  }
}


以前のバージョンの MyApp 例は、常に同じログ情報を 出力します。幸いにも、実行時にMyAppのログ出力の挙動を 変更するのは簡単です。

2番目のバージョンのMyApp は、 PropertyConfigurator を使うことで、 設定ファイルを解析して、それに従った設定をおこないます。