java 运行 JAR 时如何配置日志记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10370989/
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-10-31 00:44:38  来源:igfitidea点击:

How to configure logging when running a JAR?

javaloggingjar

提问by planky

I am new to Java logging API and need some help with this problem: While creating the application, my config file was stored in the project root folder, so I used -Djava.util.logging.config.file=logging.propertiesswitch to run the program. But then I exported the executable JAR. How to configure the logging now? It doesn't work, when I specify the path to config file with the -D switch.

我是 Java 日志 API 的新手,需要一些帮助来解决这个问题:在创建应用程序时,我的配置文件存储在项目根文件夹中,所以我使用-Djava.util.logging.config.file=logging.propertiesswitch 来运行程序。但后来我导出了可执行 JAR。现在如何配置日志记录?当我使用 -D 开关指定配置文件的路径时,它不起作用。

采纳答案by aleroot

You can't specify JVM arguments into the MANIFEST.MFfile so you have to specify the logging properties at the command line or with a shortcut :

您不能在 MANIFEST.MF文件中指定 JVM 参数,因此您必须在命令行或使用快捷方式指定日志记录属性:

java -Djava.util.logging.config.file=logging.properties -jar yourjar.jar

Otherwise you could package a properties file(logging.properties in your case) in the JAR, read that at startup and put those settings into the system properties.

否则,您可以在 JAR 中打包一个属性文件(在您的情况下为 logging.properties),在启动时读取该文件并将这些设置放入系统属性中。

回答by JB Nizet

The javadocsays:

javadoc说:

In addition, the LogManager uses two optional system properties that allow more control over reading the initial configuration:

"java.util.logging.config.class"
"java.util.logging.config.file" 

These two properties may be set via the Preferences API, or as command line property definitions to the "java" command, or as system property definitions passed to JNI_CreateJavaVM.

If the "java.util.logging.config.class" property is set, then the property value is treated as a class name. The given class will be loaded, an object will be instantiated, and that object's constructor is responsible for reading in the initial configuration. (That object may use other system properties to control its configuration.) The alternate configuration class can use readConfiguration(InputStream) to define properties in the LogManager.

此外,LogManager 使用两个可选的系统属性,可以更好地控制读取初始配置:

"java.util.logging.config.class"
"java.util.logging.config.file" 

这两个属性可以通过 Preferences API 设置,或者作为“java”命令的命令行属性定义,或者作为传递给 JNI_CreateJavaVM 的系统属性定义。

如果设置了“java.util.logging.config.class”属性,则属性值被视为类名。给定的类将被加载,一个对象将被实例化,该对象的构造函数负责读取初始配置。(该对象可能使用其他系统属性来控制其配置。)备用配置类可以使用 readConfiguration(InputStream) 在 LogManager 中定义属性。

So, either use the java.util.logging.config.filesystem property, and store the config file out of the jar file (which is probably a good idea if you want to be able to customize the logging properties in order to debug or analyze some weird behavior), or store the config file wherever you want (in the jar file, for example), and use the java.util.logging.config.classsystem property to load and instantiate a class that will read the file in the jar file (Using Class.getResourceAsStream()).

因此,要么使用java.util.logging.config.file系统属性,并将配置文件存储在 jar 文件之外(如果您希望能够自定义日志记录属性以调试或分析一些奇怪的行为,这可能是一个好主意),或者存储config 文件(例如在 jar 文件中),并使用java.util.logging.config.class系统属性加载和实例化一个类,该类将读取 jar 文件中的文件(使用Class.getResourceAsStream())。

回答by pakg1047

I know it's little late to answer this question but I ran into this issue few days back with the runnable jar and I solved it like this:

我知道现在回答这个问题有点晚了,但几天前我用可运行的 jar 遇到了这个问题,我是这样解决的:

java -Djava.util.logging.config.file=logging.properties -cp test.jar com.sample.test.Main

java -Djava.util.logging.config.file=logging.properties -cp test.jar com.sample.test.Main

where test.jar is the name of your jar and com.sample.test.Main is the fully qualified name of your main class.

其中 test.jar 是 jar 的名称,com.sample.test.Main 是主类的完全限定名称。