2016-08-01 43 views
1

我有一個使用args4j選項類(Options.java)和JUL配置類(LogConfig.java)的命令行工具(frontend.java)。因爲在我的實際應用程序中,我需要查看和使用選定的選項來配置日誌記錄,我在frontend中有一個靜態獲取器,它返回用戶選擇的各種args4j選項。應該如何(以及爲什麼)初始化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配置類需要完成?

回答

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啓動完成後自己重新創建配置類。然後,當getOptions返回null時,讓LogConfig執行無操作。

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

相關問題