加载 java.util.logging.config.file 进行默认初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/805701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:41:08  来源:igfitidea点击:

Load java.util.logging.config.file for default initialization

javapropertiesjava.util.logging

提问by abp

I'm trying to load a custom log.propertiesfile when my application is started.

我正在尝试log.properties在我的应用程序启动时加载自定义文件。

My properties file is in the same package as my main class, so I assumed that the -Djava.util.logging.config.file=log.propertiescommand line parameter should get the properties file loaded.

我的属性文件与我的主类在同一个包中,所以我假设-Djava.util.logging.config.file=log.properties命令行参数应该加载属性文件。

But the properties are only loaded when i specify a full absolute path to the properties file. Any suggestions how to use a relative path?

但是只有当我指定属性文件的完整绝对路径时才会加载属性。任何建议如何使用相对路径?

采纳答案by Aaron Digulla

Java logging doesn't search your whole hard disk for a file; there are very simple rules how files are looked up. You want Java to see that the two files belong to each other but you didn't say so anywhere. Since Java sees no connection between the properties file and your class other than that they are in the same folder on your disk, it can't find the file.

Java 日志不会在整个硬盘中搜索文件;如何查找文件有非常简单的规则。您希望 Java 看到这两个文件属于彼此,但您没有在任何地方说出来。由于 Java 认为属性文件和您的类之间没有任何联系,只是它们位于磁盘上的同一文件夹中,因此无法找到该文件。

-Djava.util.logging.config.file=log.propertiesonly works if the file log.propertiesis in the current directory of the Java process (which can be pretty random). So you should use an absolute path here.

-Djava.util.logging.config.file=log.properties仅当文件log.properties位于 Java 进程的当前目录中时才有效(这可能非常随机)。所以你应该在这里使用绝对路径。

An alternate solution would be to move the file logging.propertiesinto $JAVA_HOME/lib/(or edit the file which should be there). In that case, you don't need to set a System property.

另一种解决方案是将文件logging.properties移入$JAVA_HOME/lib/(或编辑应该在那里的文件)。在这种情况下,您不需要设置 System 属性。

回答by user280033

util logging does not load from classpath, it needs an absolute path which is why other logging packages like log4j are far easier to configure and better for web apps where it's a pain to get abs paths.

util 日志不会从类路径加载,它需要一个绝对路径,这就是为什么像 log4j 这样的其他日志包更容易配置并且更适合 Web 应用程序,因为 Web 应用程序很难获得 abs 路径。

this is not explained at all in the java.util.logging.LogManager doco.

这在 java.util.logging.LogManager doco 中根本没有解释。

回答by user280033

You can dynamically load java.util.loggingproperties files from a relative path very easily. This is what I put inside a static {}block in my Mainclass. Put your logging.propertiesfile in the default packageand you can access it very easily with the following code.

您可以java.util.logging非常轻松地从相对路径动态加载属性文件。这是我static {}Main课堂上放在一个块中的内容。将您的logging.properties文件放在 中default package,您可以使用以下代码轻松访问它。

final InputStream inputStream = Main.class.getResourceAsStream("/logging.properties");
try
{
    LogManager.getLogManager().readConfiguration(inputStream);
}
catch (final IOException e)
{
    Logger.getAnonymousLogger().severe("Could not load default logging.properties file");
    Logger.getAnonymousLogger().severe(e.getMessage());
}