Logging tools

In the preceding sections of this chapter, we learned about the importance of logging. We also learned logging best practices. Now is the time to add logging tools to our skill sets. This section focuses on logging tools. Logging tools are helpful because of the features they provide. In the past, log files consisted of log statements, written in a plain text format. Plain text log files are still useful in specific situations, like analyzing infrastructure data, but they are no longer sufficient in logging information for an application. Java has built-in support for standard logging in the java.util.logging API. Log4j is another well-known and widely used logging tool in the Java community.

Before we jump into the details of logging tools, it is important to understand the key elements of the logging mechanism. The following are key logging components:

  • Log Level: The Java logging levels are used to control the logging output. They provide flexibility in enabling or disabling the various logging levels. This makes it possible to choose which logs will be displayed in the log files. With this, it is possible that the application running on the production has a different logging level than the same application running on the staging environment. Enabling one level of logging will make all higher-level logs enabled for printing in the log files. The following are the log levels and effective logging levels for the Java logging API:

    Request level

    Effective logging level

    SEVERE

    WARNING

    INFO

    CONFIG

    FINE

    FINER

    FINEST

    SEVERE

    Yes

    Yes

    Yes

    Yes

    Yes

    Yes

    Yes

    WARNING

    No

    Yes

    Yes

    Yes

    Yes

    Yes

    Yes

    INFO

    No

    No

    Yes

    Yes

    Yes

    Yes

    Yes

    CONFIG

    No

    No

    No

    Yes

    Yes

    Yes

    Yes

    FINE

    No

    No

    No

    No

    Yes

    Yes

    Yes

    FINER

    No

    No

    No

    No

    No

    Yes

    Yes

    FINEST

     

    No

    No

    No

    No

    No

    No

    Yes

     

  • Logger: The job of the Logger object is to log application messages. The application can create anonymous loggers, which are stored differently than in the Logger namespace. The application must be sure to keep a reference to the Logger object, as the Logger may get garbage collected at any point in time. The Logger object is associated with a parent Logger object, which is the nearest ancestor in Logger namespace. During the logging process, log messages are sent to Handler objects. The Handler objects forward the log messages to files, logs, or consoles. Every Logger object has a log level associated with it. It indicates the minimum level Logger will print logs for.

  • Handler: The responsibility of a Handler object is to get log messages from Logger objects, and send those log messages for printing to the appropriate destination. Examples include writing the log messages on the console, writing the log messages into a file, or writing the log messages to a network logging service. It is possible to enable or disable a Handler, which, in essence, stops printing those logs on the output medium.

  • Formatter: The log Formatter formats the log messages before writing them to the output medium. Java supports two types of Formatter objects: SimpleFormatter and XMLFormatter. The XMLFormatter object is required to include a head and tail around formatted records. It is also possible to create custom Formatter objects.

  • LogManager: LogManager is a singleton object, used to maintain a shared state of loggers and log services. Apart from this, the LogManager object manages logging properties and the Logger namespace. The LogManager object is instantiated while class initialization takes place. The object cannot be subsequently changed. LogManager reads the initial configuration from the lib/logging.properties file by default, which can be modified.

The following diagram shows the logging process with one Handler:

The following diagram shows the logging process with multiple handlers:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset