Classes for logging purposes.
The developer can place any logs into code to inform other developers or the users about something.
There are several logging frameworks with full support for logging, e.g. Log4j (Apache), SLF4J (abstraction layer for different kind frameworks), Logback (open-source version of Log4j ver1), tinylog (light weight open-source).
Loggers are conventionally named after their carrier classes. The variable name is different from the name in name space which will be the class name.
In case a class is in a hierarchical system like in a package, the name should follow the system e.g. java.awt or java.util.
Log records are categorized into levels.
Running a logger on a level means all log records on the defined or higher levels are passed through while under the defined level are dropped.
EXAMPLE FOR DECLARING A LOGGER AND SETTING ITS LEVEL:
EXAMPLE FOR CREATING LOG EVENTS:
In any case of exceptions, it is advised to create an event with all the data available about the exception.
EXAMPLE FOR LOGGING AN EXCEPTION:
For a class, it is a good practice to put a logger declaration into the first declarations, and upon extending, implementing or importing, adding them to the main class.
Next to .log, there are two other methods for creating an event:
.logp: same as .log but takes class name and method name as arguments as well;
.logrb: same as .logp but takes the resource bundle name as argument as well.
Every log event is presented as a LogRecord.
It holds the time of event, the text of event, the module from which the event has been raised etc.
A LogRecord is created by a Logger, and sent to a Handler.
For every stored (carried) information the LogRecord has getter method.
Although there is possibility to create an authentic LogRecord and set its information, for logging purposes it is better to create an event through Logger instead.
In case of customization, the passed LogRecords are caught and modified via overridden methods, see later.
The raised log records will be forwarded to one or more log handlers (appenders) which handle the output channels.
A ConsoleHandler is already added to a new Logger by default.
It can be removed from the Logger.
In case there is probability that the class will be used in an other class with LogManager (see later), should avoid rendering a handler to the Logger of the class.
EXAMPLE FOR DECLARING AND RE-ORGANIZING LOG HANDLERS:
By extending, a handler can be customized.
EXAMPLE FOR CUSTOM HANDLER:
Filters can be defined; a log record can be passed to a handler only if conditions met.
Formatted log records can be set through formatters (layouts). SimpleFormatter is default for ConsoleHandler while XMLFormatter is for FileHandler, but SimpleFormatter can be used for files as well.
By extending a formatter, customization is possible.
EXAMPLE FOR CREATING A CUSTOM FORMATTER FROM SIMPLEFORMATTER:
The LogManager is a singleton, by importing its class it is already set up. It tracks loggers globally.
Every defined logger can be added to the LogManager.
All added loggers bring their settings with them which means the application can have several formats etc. for log records. In order to create a standard for the application, all added loggers can be reached through LogManager.
The manager has a nameless and a global logger by default, then the added loggers follow. By default, the empty-named logger has an already-added ConsoleHandler while the global logger has none.
In order to control the added loggers globally, use the nameless logger of the manager, modify its levels and handlers.
Alternatively, remove the default ConsoleHandler from the nameless logger, and add one or more handler to the global logger.
In order to set added loggers independently but with iteration, remove the default ConsoleHandler from the nameless logger of the manager.
In order to let the classes manage their own loggers, accessing the LogManager is not necessary.
In case handlers are added to the added loggers of added classes, should be removed first for unification, or parallel usage of global (nameless) logger results double (and probably in different format) logging.
EXAMPLE FOR LOGGER DECLARATION IN A CLASS IN ORDER TO USE IT WITH LOGMANAGER IN A CALLER APPLICATION:
EXAMPLE FOR SETTING UP LOGMANAGER WITH DEFAULTS:
EXAMPLE FOR SETTING UP THE NAMELESS LOGGER OF LOGMANAGER: