java.util.logging.Logger的级别与根Logger的级别不同 [英] java.util.logging.Logger has different Level than it's root Logger

查看:291
本文介绍了java.util.logging.Logger的级别与根Logger的级别不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我一开始将java.util.logging的根记录器及其所有处理程序设置为Level.FINEST,如下所示:

In my application at one point I set the root Logger of java.util.logging and all it's handlers to Level.FINEST like this:

Logger.getLogger("").setLevel(Level.FINEST);
for (Handler handler : Logger.getLogger("").getHandlers()) {
      handler.setLevel(Level.FINEST);
}

稍后,我将创建一个类Logger,如下所示:

In a later point I create a class Logger like this:

private static final Logger JAVA_LOG = Logger.getLogger(MyClass.class.getName());

现在此记录器级别为INFO(这似乎是JUL的默认值),而其根记录器级别已正确设置为FINEST.不应为根记录器设置级别,也不要为根记录器的所有子记录器设置级别?检索Logger类时我做错什么了吗?

Now this loggers Level is INFO (which seems to be the default for JUL), while it's root Loggers Level is properly set to FINEST. Shouldn't setting the Level for the root Logger also set the Level for all children loggers of the root Logger? Am I doing something wrong in retrieving the class Logger?

推荐答案

是否应该为根Logger设置级别,也为根Logger的所有子记录器设置Level?

否,显然您正在创建一个新的Logger对象. Javadoc对于getLogger 状态:

No, apparently you are creating a new Logger object. Javadoc for getLogger states:

如果创建了新的记录器,则其日志级别将基于 LogManager配置,它将配置为也发送 将输出记录到其父级的处理程序中.

If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's Handlers.

请注意,即使以前没有使用相同名称的Logger对象,也不能依靠getLogger返回相同的对象,因为如果没有对该对象的强引用,则该对象可能已经被垃圾回收了.

Please note, that even if there had been a Logger object with the same name before, you cannot rely on getLogger to return the same object as it might have been garbage collected if there is no strong reference to the object.

如果您的目的是在项目中创建具有相同日志级别的所有Logger对象,则不应以编程方式进行操作,而应通过配置进行操作.那就是java.util.logging派上用场的地方.总体优势在于,您可以在不同的日志级别运行应用程序,而无需更改任何代码.

If your aim is to create all Logger objects within your project with the same log level, you should not do it programmatically but by configuration. That is where java.util.logging comes in handy. The overall advantage is that you can run your application in different log levels without even changeing a single line of code.

只需编辑您的配置文件(本质上就是上面提到的LogManager configuration).它称为logging.properties,位于您的JRE目录中.在那里,您可以编辑最后一段并配置项目范围或狭窄类别的日志级别:

Simply edit your configuration file (which essentially is the above mentioned LogManager configuration). It is called logging.properties and located in your JRE directory. There you can edit the last paragraph and configure project-wide or class-narrowed log levels:

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

myProject.MyClass.level = FINE
myProject.MyOtherNotSoImportantClass.level = INFO
mySecondProject.MainClass.level = NONE

一个更好的解决方案是将logging.properties复制到您的项目目录并使用

An even better solution is to copy logging.properties to your project directory and call your app with

java -Djava.util.logging.config.file=./src/test/resources/logging.properties

这篇关于java.util.logging.Logger的级别与根Logger的级别不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆