Extend JDK Log Framework

Source: Internet
Author: User
Tags filter extend socket

Starting with Java 1.4, the JDK includes a brand-new log frame package java.util.logging, which is well designed, tightly integrated with JDK, flexible and easy to use. Logging is important for software maintenance, especially for software debugging that has been deployed to a running environment. In actual projects, more complex logging features are often needed than the framework provides. For this requirement, the JDK log framework has sufficient scalability to customize components such as log processing of different requirements, message formatting, logging message levels, and so on. In the following sections, this article describes how to extend the JDK log framework and customize the way log is handled. A practical example is presented to monitor Java programs with the JDK log framework and the STAF (Software testing Automation framework, an automated test framework) logging service.

Introduction to JDK Log framework

The JDK's log frame is the java.util.logging package. For a software log system, you must first have a log object that is responsible for logging information. The information can also be exported to different locations, such as consoles, files, and even networks. For the format of the information, it can be printed in plain text, XML, or HTML format depending on the requirements. There is also a need for different levels of classification of log information, which benefits from filtering redundant information and retaining only critical logs. For a log frame, the log object must be configurable, and it can be output to a specified target as configured, while the format of the output is determined according to the configuration and the log at which level above is determined to output. The configuration can also be a variety of forms, either in the form of code or in the form of a configuration file. In particular, the form of a configuration file, for a software that has already been deployed to a running environment, can easily change the log configuration without changing its source code.

The JDK log Framework provides all of the above features. It mainly includes the following parts:

Logger: Logging object. Used to record log information.

Handler: The output used to process log information. In the Handler class, you can decide whether the log is output to a file or a console.

Filter: for filtering logs. In the Filter class, you can decide whether to output the log based on the level of the log or some condition. This achieves the purpose of removing redundant information.

Formatter: Used to format log information. This class can format the log text into XML or HTML format, which depends entirely on the implementation.

Level: Used to represent levels of the log. The JDK log framework defaults to the following levels: Severe,warning,info,config,fine,finer,finest.

For a program, its Logger object first determines whether the log level meets the output level requirements, and then gives the log messages that meet the level requirements to the configured Handler object for processing, and if the log object is configured with a filter object, the filter The object will filter the log information once. After the Handler object receives a log message, it changes the format of the log according to the formatted class Formatter it configures, filters the log information again based on the configured filter object and level object, and outputs it to the output location specified by the Handler object. The output location can be a console, a file, a network socket, or even a memory buffer. Its architectural model is shown in Figure 1.

Figure 1 JDK Log Framework

The JDK provides several Handler classes that are supported by default:

Consolehandler: Output Log to console

Filehandler: Output log to specified file

Memoryhandler: Output log to memory buffer, when certain conditions are met (such as log information for a keyword) and then output the log in the buffer

Sockethandler: output log to network socket

Streamhandler: Output log to input output stream object

While the JDK log framework is also flexible, you can customize the Handler you need, output the log to a different location in accordance with custom requirements, and Formatter, the level class can customize the extension, the following details how to customize the extension of these components.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.