Java 中是否有用于设置默认日志级别的命令行选项
我可以做一些类似的事情吗:
-Djava.util.logging.loglevel=FINE
显然这不起作用,但你明白了。有这样的事吗?或者我被迫创建一个属性文件?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以做一些类似的事情吗:
-Djava.util.logging.loglevel=FINE
显然这不起作用,但你明白了。有这样的事吗?或者我被迫创建一个属性文件?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
tldr:
对于 java-9+ 将此代码放在
main
方法的开头:(请参阅 java-8 及更早版本的底部)
如果
main
无法修改的解释和解决方法:通常
java.util.logging
不使用系统属性来配置自身(有有一些例外,例如 java.util.logging.SimpleFormatter.format)。相反,全局 LogManager 单例实例负责配置日志子系统,默认情况下它从${JAVA_HOME}/conf/logging.properties
加载属性。有几种方法可以调整此行为:java.util.logging.config.file
系统属性以及替代日志配置属性文件的路径。java.util.logging.config.class
系统属性,其类名完全覆盖配置过程(通常通过提供自定义InputStream
并将配置属性记录到 readConfiguration(is))。因此,如果要覆盖全局根日志级别,最简单的方法可能是在
main
方法开头的某个位置使用updateConfiguration(mapper)
。 LogManager 使用 .level 属性(空字符串根记录器的级别)作为未显式配置的子记录器的默认值,因此使用 java.util。来自OP的logging.loglevel系统属性,它会像这样:请注意,在默认日志记录配置的情况下,上述内容对于任何
FINE
(或更低)日志条目是不够的在控制台上输出,如 ConsoleHandler 默认输出INFO
及更高版本。要更改此设置,您还需要重写 java.util.logging.ConsoleHandler.level 日志记录配置属性。如果您想再次使用相同的系统属性,则需要修改updateConfiguration
的参数,如下所示:如果您无法更改
main
方法,那么您可以定义前面提到的 java.util.logging.config.class 属性来指向您的类,如下所示:-Djava.util.logging.config.class=com.example。 MyJulConfigurator。这会将记录配置的任务委托给此类的构造函数。在这样的构造函数中,您可以首先以正常方式从文件中读取配置(使用 readConfiguration() 方法),然后使用前面代码片段中的代码:最后一点,我最近编写了一个简单的帮助函数可以更轻松地对日志配置进行任何临时命令行更改:overrideLogLevelsWithSystemProperties(还支持添加新的日志记录属性)。
正如 @TWiStErRob 在 注释,您甚至不需要包含包含的
jul-utils
作为项目的依赖项:只需将其添加到您的类路径(可在 < a href="https://search.maven.org/artifact/pl.morgwai.base/jul-utils/" rel="nofollow noreferrer">central) 启动应用程序时并定义所需的系统属性:java-8 及更早版本的概述:
LogManager
确定要读取的文件)ByteArrayOutputStream
中,使用 store(os , null)ByteArrayInputStream
(另请参阅此讨论)并将其传递给 readConfiguration(is)和以前一样,您可以在您的
main
方法或在java.util.logging.config.class
系统属性指向的某个类的构造函数中。tldr:
for java-9+ put this code at the beginning of your
main
method:(see the bottom for java-8 and earlier)
explanation and workaround if
main
cannot be modified:Generally
java.util.logging
does not use system properties to configure itself (there are a few exceptions, likejava.util.logging.SimpleFormatter.format
). Instead, the global LogManager singleton instance is responsible for configuring the logging subsystem and by default it loads properties from${JAVA_HOME}/conf/logging.properties
. There are a few ways to tweak this behavior:java.util.logging.config.file
system property with a path to an alternative logging config properties file.java.util.logging.config.class
system property with a class name that completely overrides configuration process (usually by providing a customInputStream
with logging config properties to readConfiguration(is)).So in case of overriding the global root log-level, it's probably easiest to use
updateConfiguration(mapper)
somewhere at the beginning of yourmain
method.LogManager
uses.level
property (level of the empty-string root logger) as a default for its child loggers not configured explicitly, so using thejava.util.logging.loglevel
system property from the OP, it would be like this:Note that in case of the default logging config, the above is not sufficient for any
FINE
(or lower) log entries to be output on the console, as ConsoleHandler by default outputsINFO
and higher. To change this, you need to override alsojava.util.logging.ConsoleHandler.level
logging config property. If you want to use the same system property for this again, then you need to modify the argument ofupdateConfiguration
like the below:If you can't change the
main
method, then you can define the previously mentionedjava.util.logging.config.class
property to point to your class like this:-Djava.util.logging.config.class=com.example.MyJulConfigurator
. This will delegate the task of logging configuration to the constructor of this class. In such constructor you can first read the config from a file the normal way (using readConfiguration() method) and then use the code from the previous snippet:As a final note, I've recently written a simple helper function to make any ad-hoc command-line changes to logging config easier: overrideLogLevelsWithSystemProperties (also supports adding new logging properties).
As mentioned by @TWiStErRob in the comment, you don't even need to include the containing
jul-utils
as a dependency of your project: just add it to your classpath (available in central) when starting your app and define your desired system properties:outline for java-8 and earlier:
Properties
using load(is) (see here howLogManager
determines which file to read).level
andjava.util.logging.ConsoleHandler.level
using setProperty(key, value) with the value obtained from the system propertyByteArrayOutputStream
using store(os, null)ByteArrayInputStream
(see also this discussion) and pass it to readConfiguration(is)as before, you can do it either in your
main
method or in a constructor of some class pointed byjava.util.logging.config.class
system property.您甚至可以将日志级别作为用户定义的属性传递。
在您的代码中:
但我认为您正在寻找更“内置”且自动处理的属性,对吗? AFAIK,它不存在,但也许我错了。
You can even pass your log Level as a user defined property.
In your code:
But I have the idea that your are looking for a more "built-in" and automatically handled property, right? AFAIK, it doesn't exist, but maybe I'm wrong.
您可以配置代码以根据环境变量设置级别:
you can configure your code to set the level based on an envrioment variable :