Java provides advanced logging functionality through the
Creating An Instance
It is common practise to declare a single (static) instance of a Logger per class. This technique also forgoes dependency injection.
The single parameter passed to the Logger constructor defines both the name and hierarchy for the Logger. It is common practice to use the fully qualified class name for this purpose (e.g.
myPackage.LoggingExample). An easy way to do this is to just use the method
A Logger object actually passtes all of it’s logging data to a Handler object, which then outputs the log data to wherever it is needed.
A Handler object has it’s own priority. Even though the Logger’s level might be set to Level.ALL, if the Handler object’s level is only set to Level.INFO, then the log data will never be “logged”.
Hierarchy And Record Forwarding
Logger objects support (and are aware of) their hierarchy. Logger objects will send output log data (a LogRecord) to all attached handlers, and to their parent (or higher ancestor) Logger object (if any). This is called record forwarding.
The parent Logger does not perform any level or filter checks, but instead immediately forwards the LogRecord onto all of it’s handlers, and any parent Logger objects.
The LogManager object is a central control room for all Logger objects within your application. Only one instance can exist, and can be obtained by calling:
The LogManager object can be used to set all Logger objects within a certain package to a specific Level.
This work is licensed under a Creative Commons Attribution 4.0 International License .