2016-08-01 3 views
1

私はargs4jオプションクラス(Options.java)とJUL構成クラス(LogConfig.java)を使ってコマンドラインツール(frontend.java)を持っています。私の実際のアプリケーションでは、ロギングを設定するために選択したオプションを確認して使用する必要があるため、ユーザが選択したさまざまなargs4jオプションを返すstatic getterをfrontendに持っています。JUL構成クラスをどのように初期化する必要がありますか?

frontend.java: 
import java.util.logging.Logger; 

public class frontend{ 
    private static Logger log = Logger.getLogger(frontend.class.getName()); 
    private static Options opts = new Options(); 

    public frontend(){ 
     System.out.println("Made a new front end"); 
    } 
    public static Options getOptions(){ 
     if(opts == null) 
      System.out.println("Opts was null"); 
     else 
      System.out.println("Opts is not null"); 
     return opts; 

    } 
    public static void main(String[] args) { 
     frontend fe = new frontend(); 
    } 
} 

明らかに、この次のファイルは実際には空白ではありませんが、私の問題点を示すためにここに何かが必要なのか分かりません。

Options.java: 
public class Options{ 
} 

そして最後に、コンフィギュレーション・クラス:

LogConfig.java: 
import java.util.logging.LogManager; 
import java.util.logging.Logger; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

public class LogConfig { 
    public LogConfig() { 
     Options opts = frontend.getOptions(); 
     try { 
      LogManager.getLogManager().readConfiguration(new FileInputStream("logging.properties")); 
     }catch (SecurityException e) { 
      System.err.println("Problem accessing log config file: " 
           + e.getMessage()); 
     } catch (IOException e) { 
      System.err.println("Problem loading log config file: " 
           + e.getMessage()); 
     } 
    } 
} 

私の問題はLogConfigクラスは、それが静的な初期化によって設定されています前に、opts変数を手に入れるように見えるということです。

Output: 
c:\>java -cp . frontend 
Made a new front end 
Opts is not null 

c:\>java -Djava.util.logging.config.class=LogConfig -cp . frontend 
Opts was null 
Made a new front end 
Opts is not null 

c:\> 

JULログ設定クラスを使用する最良の方法は、メインクラスがいくらか生きていなければならないときに、あなたのconf igurationクラスは達成する必要がありますか?

答えて

1

Loggerを作成すると、LogManagerが起動します。あなたが使用するまでロガーの作成を遅らせる。

public class frontend { 

    private static volatile Logger log; 
    private static Options opts = new Options(); 

    public frontend() { 
     System.out.println("Made a new front end"); 
    } 

    public static Options getOptions() { 
     if (opts == null) { 
      System.out.println("Opts was null"); 
     } else { 
      System.out.println("Opts is not null"); 
     } 
     return opts; 

    } 

    public static void main(String[] args) { 
     frontend fe = new frontend(); 
     log = Logger.getLogger(frontend.class.getName()); 
    } 
} 

そうしないと、LogManagerの起動が完了した後に、自分でconfigクラスを再作成することができます。次に、getOptionsがnullを返したときにLogConfigをno-opで実行します。

public class frontend { 

    private static final Logger log = Logger.getLogger(frontend.class.getName()); 
    private static Options opts = new Options(); 

    public frontend() { 
     System.out.println("Made a new front end"); 
    } 

    public static Options getOptions() { 
     if (opts == null) { 
      System.out.println("Opts was null"); 
     } else { 
      System.out.println("Opts is not null"); 
     } 
     return opts; 

    } 

    public static void main(String[] args) { 
     frontend fe = new frontend(); 
     init(); 
    } 

    private static void init() { 
     String n = System.getProperty("java.util.logging.config.class"); 
     if (n != null) { 
      try { //LogManager uses the system class loader. 
       Class<?> k = Class.forName(n, false, 
         ClassLoader.getSystemClassLoader()); 
       k.newInstance(); 
      } catch (ReflectiveOperationException | LinkageError ignore) { 
       ignore.printStackTrace(); 
      } 
     } 
    } 
} 
+0

ありがとうございました、実際には私がやったことです。つまり、ログの設定を 'frontend'の初期化に置くことです。多くの場合、これはjava.util.logging.config.classとグローバルな "config"プロパティの概念を取り除いているようです。あなたがここにいる(そして私がしたことは)私には "自由な"設定処理をバイパスしているように思えます。 OOTBの "java.util.logging.config.class"メカニズムまたはイントロスペクションのどちらかを使用するのではなく、LogConfig.configure()に相当するだけでした。私は、構成クラスがフロントエンドクラスにアクセスすることができないということを覚えています。 – AndyJ

関連する問題