Frage

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. The documentation for Logger.getGlobal() just says

Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.

Logger.GLOBAL_LOGGER_NAME, in turn, is documented only as

GLOBAL_LOGGER_NAME is a name for the global logger.

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?

War es hilfreich?

Lösung

The "global" Logger object is provided as a convenience to developers who are making casual use of the Logging package. 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. Developers also need to keep a strong reference to their Logger objects to prevent them from being garbage collected.

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.

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.

Andere Tipps

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");'

Answer is that we use class named loggers for convenience, e.g. 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.

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.

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();

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top