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

发布于 2024-07-18 11:22:52 字数 207 浏览 5 评论 0原文

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

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

但仅当我指定属性文件的完整绝对路径时才会加载属性。 关于如何使用相对路径有什么建议吗?

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦欢 2024-07-25 11:22:52

您可以非常轻松地从相对路径动态加载 java.util.logging 属性文件。 这是我在 Main 类中的 static {} 块中放置的内容。 将您的 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());
}

You can dynamically load java.util.logging properties files from a relative path very easily. This is what I put inside a static {} block in my Main class. Put your logging.properties file in the default package and you can access it very easily with the following code.

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());
}
開玄 2024-07-25 11:22:52

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

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

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

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.

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

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

心的憧憬 2024-07-25 11:22:52

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文