简体   繁体   English

在java.util.logging中,什么是全局记录器?

[英]In java.util.logging, what is the global logger for?

In the java.util.logging logging framework there's a special Logger instance named "global" , but I can't find any documentation of what its intended use is. java.util.logging日志框架中有一个名为"global"的特殊Logger实例,但我找不到任何有关其预期用途的文档。 The documentation for Logger.getGlobal() just says Logger.getGlobal()的文档只是说

Return global logger object with the name Logger.GLOBAL_LOGGER_NAME . 返回名为Logger.GLOBAL_LOGGER_NAME全局记录器对象。

Logger.GLOBAL_LOGGER_NAME , in turn, is documented only as 反过来, Logger.GLOBAL_LOGGER_NAME仅记录为

GLOBAL_LOGGER_NAME is a name for the global logger. GLOBAL_LOGGER_NAME是全局记录器的名称。

My fairly extensive searches didn't turn up any more useful documentation. 我相当广泛的搜索没有找到任何更有用的文档。

What is the global logger intended to be used for? 什么是全球记录器用于什么? Is that documented somewhere that I missed? 这是在我遗漏的地方记录的吗?

The "global" Logger object is provided as a convenience to developers who are making casual use of the Logging package. 提供“全局”Logger对象是为了方便正在使用Logging包的开发人员。 Developers who are making serious use of the logging package (for example in products) should create and use their own Logger objects, with appropriate names, so that logging can be controlled on a suitable per-Logger granularity. 正在认真使用日志包(例如在产品中)的开发人员应该创建和使用他们自己的Logger对象,并使用适当的名称,以便可以在适当的每Logger粒度上控制日志记录。 Developers also need to keep a strong reference to their Logger objects to prevent them from being garbage collected. 开发人员还需要对其Logger对象保持强引用,以防止它们被垃圾回收。

From here (don't look at the fact that the field is deprecated, I just wanted to point you to a valid explanation). 这里开始 (不要看这个字段被弃用的事实,我只是想指出一个有效的解释)。

Normally, when enabling logging in an application, you define more granular loggers, usually per Java packages or classes. 通常,在应用程序中启用日志记录时,您可以定义更精细的记录器,通常是每个Java包或类。

If you do not want to do that, you can use this global logger, which will handle all logging statements, no matter the library, package or class they are contained in. 如果您不想这样做,可以使用此全局记录器,它将处理所有日志记录语句,无论它们包含在哪个库,包或类中。

I believe that main question not "why do we have global logger", but 'why do we have class specified loggers and use getLogger() method and why we use it like - Logger.getLogger("package.className");' 我认为主要问题不是“为什么我们有全局记录器”,而是'为什么我们有类指定的记录器并使用getLogger()方法以及为什么我们使用它 - Logger.getLogger(“package.className”);'

Answer is that we use class named loggers for convenience, eg you want to log some specific class, and you want to use debug level on this class and info level on others classes, to see debug messages only from this class, you can adjust logging properties to debug only this class. 答案是我们使用类名为loggers的方便,例如你想记录一些特定的类,你想在这个类和其他类的信息级别上使用调试级别,只能从这个类看到调试消息,你可以调整日志记录仅调试此类的属性。

Also you can have some abstract class and there could be some logger in it, and you may don't want to use same logger in all inherited classes, you may want to override logger in your inherited class and use some specific level for it. 你也可以有一些抽象类,里面可能有一些记录器,你可能不想在所有继承的类中使用相同的记录器,你可能想要在你继承的类中覆盖logger并为它使用一些特定的级别。

I believe this is very useful. 我相信这非常有用。 And answer to you question, if you have small project, and you don't want to use class named loggers, and use only one global logger, then you can use this Logger by running Logger.getGlobal(); 并回答你的问题,如果你有一个小项目,并且你不想使用类命名记录器,并且只使用一个全局记录器,那么你可以通过运行Logger.getGlobal()来使用这个记录器;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM