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配置类需要完成?
谢谢,这实际上就是我所做的 - 即将日志配置放入'frontend'初始化中。在我看来,在许多情况下,这可以排除java.util.logging.config.class的概念以及全局的“config”属性。你在这里(和我所做的)在我看来是绕过了“免费”配置处理。我没有使用OOTB“java.util.logging.config.class”机制或内省,而只是做了相当于LogConfig.configure()的操作。我想这个问题是配置类不能访问前端类。 – AndyJ