Design and implementation of log system framework

Source: Internet
Author: User
Tags string format log4j

In the Java realm, there are a large number of log components, and Open-open contains 21 log components. As an application service, the log system plays an important role in tracking debugging, program status record, crash data recovery, and we can consider the Java log system as an essential tracing debugging tool.

1. Introduction

The log system is an indispensable tool for tracking debugging, especially in any unattended background program and in systems that do not track the debugging environment. For a long time, as a kind of application service, log system has very practical significance for track debugging, program status record and crash data recovery. This kind of service usually exists in two ways:

1. The log system exists as a service process. The Event Log service in Windows is of this type, and the type of log system usually sends the log from the log to the log service through the Message Queuing mechanism. The log sender and log save side are usually not in the same process, and the log is sent as an asynchronous process. This logging service is typically used by administrators to monitor the status of various system services.

2. The log system exists as a system call. The log systems in the Java world and the log systems used by many daemons in the UNIX environment fall into this category. The log system's code is compiled into the log sending end as a system call, and the running of the log system and the running of the business code are in the same process space. The sending of logs is mostly a synchronization process. This logging service is typically used for debug tracing and crash recovery because it can synchronously reflect the state of the system running.

The log system established in this article is basically the second type, but it is different. The log system will utilize Java Threading Technology to implement a synchronous log send process that reflects the running state of a program in a unified thread space, provide a fast logging service, and provide a flexible log format configuration and filtering mechanism.

1.1 Error of System debugging

When debugging a Java program on a console environment, outputting a paragraph of text to the console or text file is the easiest way to view the running state of a program, but this does not solve all the problems. Sometimes a good log system is necessary for a system where we can't see the output of the system in real time or a system that really needs to keep our output information. Therefore, can not arbitrarily output a variety of nonstandard debugging information, these arbitrary output information is not controllable, difficult to clear, may be background monitoring, error removal and error recovery bring considerable resistance.

1.2 Basic functions of the log system framework

A complete log system framework should typically include the following basic features:

The log output has its own classification: This makes it easier to query for different modules of different systems while debugging, and thus quickly navigates to the code where the log event occurs.

The log is divided into different levels according to a standard: post-graded logs, which can be used for log filtering under the same category.

Support for Multithreading: Logging systems are often used in multithreaded environments, especially in Java systems, so as a system resource, logging systems should be guaranteed to be thread-safe.

Support for different recording media: Different projects often have different recording media requirements for logging systems, so the logging system must provide the necessary development interface to ensure that the recording media can be easily replaced.

High performance: The log system usually provides a high speed logging function to cope with the normal operation of the system under the large request flow under the large system.

Stability: The logging system must maintain a high degree of stability and cannot cause major business code crashes due to internal errors in the log system.

1.3 Common Log System Introduction

In the Java world, the following three log frameworks are excellent:

1) log4j

One of the earliest Java log frameworks, launched by the Apache Foundation, provides a flexible and powerful logging mechanism. But its complex configuration process and internal concepts often discourage users.

2) Jdk1.4loggingframework

Following the log4j, the JDK Standards Board log4j the basic ideas of the JDK, releasing the first log frame interface in JDK1.4 and providing a simple implementation.

3) Commonsloggingframwork

The framework is also the Apache Foundation project, which appears primarily to allow Java projects to switch freely between log4j and jdk1.4lloggingframework, so the framework provides a unified invocation interface and configuration method.

2. System Design

Because log4j is widely used, from the user's point of view, the framework designed in this paper uses some log4j interface and concept, but the internal implementation is completely different. The key technology for using the Java Implementation Log framework is the internal implementation of the log framework features mentioned earlier, in particular: classification and level of logs, design of the journal Distribution Framework, design of the logger, and consideration of high performance and stability in design.

2.1 System Architecture

The log system framework can be divided into two parts: logging module and log output module. The logging module is responsible for creating and managing the Logger (Logger), where each Logger object receives a variety of log objects (Logitem) that record log information at different levels (Loggerlevel), and the Logger object first obtains all logs that need to be logged. and synchronously assigns the log to the log output module. The log output module is responsible for the creation and management of the log output (Appender) and the output of the log. There are several different log outputs allowed in the system, and the log output is responsible for logging to the storage medium. The system structure is shown in Figure 1 below:

  

Figure 2 below shows the architecture of the log system using a UML class diagram:

  

In the architecture given in Figure 2, the logger logger is the user interface of the entire log system framework, the programmer can log through the interface, in order to achieve the classification of the log, the system design allows multiple logger objects, each logger responsible for a class of log records, The logger class also implements the management of its object itself. The Loggerlevel class defines the level of the entire logging system, which is used when the client creates and sends logs. When a logger object receives a log message that the client creates and sends, it also wraps the log message into the log object Logitem used within the log system, which, in addition to the message sent by the sender, wraps such things as the name of the sending end class, sending the event, sending the method name, sending the line number, and so on. These additional messages are valuable for system tracking and debugging. Packaged Logitem are eventually sent to the output, which is responsible for writing the log information to the final media, the type and number of the output is not fixed, all the output through the Appendermanager management, usually through the configuration file can be easily extended out of multiple output.

2.2 Design of the logging section

As mentioned earlier, the log section is responsible for receiving the log messages sent by the log system client and managing the log objects. The following is a detailed description of the design Essentials for the logging section:

1. Management of the log recorder

The system classifies records by keeping multiple logger objects in the same way. Each logger object represents a class of log classifications. Therefore, the Name property of the Logger object is its unique identity, and a logger object is obtained by using the Name property:

1.loggerloggerlogger=logger.getlogger ("Loggername");

In general, using the class name as the name of the logger is the advantage of minimizing the conflict between the log recorder naming (because Java classes use the package name) and the ability to classify logging as finely as possible. Therefore, assuming that there is a Usermanager class that requires the use of log services, the more general way to use it is:

2.loggerloggerlogger=logger.getlogger (Usermanager.class);

2. Implementation of log grading

Depending on the log purpose, the level of the log is divided into five levels from low to High:

debug-indicates that the output log is a debug information;

Info-indicates that the output log is a system hint;

warn-indicates that the output log is a warning message;

error-indicates that the output log is a system error;

fatal-indicates that the output log is a serious error causing the system to crash.

These log levels are defined in the Loggerlevel interface and are used internally by the logger logger. For log system clients, the logger class interface can be used to directly invoke and output these levels of logs, logger these interfaces are described as follows:

3.publicvoiddebug (stringmsg)//Output debugging information

4.publicvoidinfo (stringmsg)//output system hint

5.publicvoidwarn (stringmsg);//Output warning message

6.publicvoidfatal (stringmsg)//output system error

7.publicvoiderror (stringmsg)//Output Critical Error

By the invocation of these interfaces on the Logger object, the level attribute is given directly to the log information, which lays the foundation for the subsequent work of outputting according to different levels.

3. Acquisition of log object information

The Log object contains all the information that a log has. Typically, this information includes the time of the output log, the Java class, the class member method, the line number, the log body, the log level, and so on. In JDK1.4, a stack of system calls can be automatically populated by the JVM in the caught exception object by throwing and capturing an exception in the method, and in JDK1.4 you can use Java.lang.StackTraceElement to get the basic information for each stack item by End output Log method calls the projection of the layer, you can easily get to the Stacktraceelement object, so that the Java class, the class member method, the line number, and so on when the output log is obtained. In JDK1.3 or earlier versions, the corresponding work must be done by outputting the stack information of the exception to the string and parsing the string format.

2.3 Log output part of the design

The log output part of the design has a certain degree of difficulty, in this paper's design of the log system, log output, multithreading support, the expansion of the log system, the efficiency of the log system and other issues are referred to the log output part of the management.

1. The inheritance structure of the log output

The two-tier structure is used in the output section of the log, which defines an abstract log renderer (Abstractloggerappender) and then inherits the actual log output from the abstract class. Abstractloggerappender defines a series of methods for filtering logs, while the specific method of outputting to the storage medium is an abstract method, implemented by subclasses. Two kinds of console output and file output are realized by default in the system, and the realization of console output is quite simple.

2. Internal implementation of the file output device

In the implementation of the logging section, multithreading and high efficiency are not considered, so the file exporter must consider the processing of these problems. The use of Java.lang.Vector within the file renderer defines a thread-safe high-speed buffer in which all logs assigned to the file output through the log section are placed directly into the cache. At the same time, the internal definition of a worker thread in the file output, responsible for periodically save the contents of the cache to the file, in the process of saving the log files can be backup and so on. Because of the high speed buffer structure, it is clear that the log client's call is no longer a synchronous call, and will no longer need to wait until the file operation to return to increase the speed of the system call. The principle is shown in Figure 3:

  

2.4 Design Difficulties

With the above design, a high performance log system with good scalability has been developed in a certain form. Several difficult problems in the process of design need further reflection.

First, whether the entire system should adopt a fully asynchronous structure, through a similar message mechanism to the log client sent log to the system. This method can be used as another mode of operation in the log system framework, which is considered in the subsequent design.

Two, in the file output can be seen, currently, although multiple log outputs can be extended, the abstract class currently provided provides only a filtering mechanism for the log, and there is no caching mechanism provided, and the current caching mechanism is implemented in the file renderer, so in future further design, You can move the caching mechanism in the file renderer to the abstract class.

2.5 design mode

In the design process we pay special attention to the use of a number of classic design patterns. For example, the creation of the Logger object uses the factory method pattern (FactoryMethod), the Abstractloggerappender and Consoleappender, and the Fileappender form the policy pattern (strategy), In addition, a large number of single case patterns (Singleton) are used. The proper use of design patterns in design can speed up the design progress and improve the design quality.

3. Summary

This paper discusses the basic characteristics of the log system, the significance, methods and internal structure of the log system, and gives a detailed design of the log system based on the Java platform. It also points out that the logging system will develop in the direction of service and Asynchrony. As a convenient tool for tracking debugging and data recovery, the use of log system should be advocated in the proper environment.

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.