Recommend a good article about the design of the Java Log System framework article: http://soft.chinabyte.com/database/438/11321938.shtml
Summary of article Content:
- The log system plays an important role in tracking debugging, program status recording, data recovery and other Functions.
- The log system generally exists as a service process or system call, and we use system calls in general programs
- The common log system includes a brief introduction to Log4j
- System architecture of the log system
- Information grading for the log system
- Design of the log output
The following is a reference to the full text:
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 state record and recovery of crash data, and we can think of the Java log system as an essential tracking and debugging Tool.
1. Introduction
Log system is an indispensable tracking debugging tool, especially in any unattended background program and those who do not track the debugging environment of the system has a wide range of Applications. For a long time, the log system, as an application service, has very practical significance for tracking debugging, program state 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 log system of that type usually sends the log sent to the log service by the log sending end through the Message Queuing Mechanism. The log send side and the log save side are usually not in the same process, and the sending of logs is an asynchronous process. This log service is typically used by administrators to monitor the status of various system Services.
2. The log system is present as a system Call. This is the type of log system that is used by many daemons in the Java world, both in the log system and in the Unix Environment. The code of the log system is compiled into the log sending side as a system call, and the log system runs and the business code runs in the same process space. The sending of logs is mostly a synchronization process. This log service is typically used for debug tracing and crash recovery because it is able to synchronously reflect the state of the system Running.
The log system established in this paper is basically the second type, but it is different. The log system will use Java Threading Technology to implement a synchronous log sending process that reflects the state of the program running in the unified thread space, provide a fast logging service, and provide a flexible log format configuration and filtering Mechanism.
1.1 Errors in System commissioning
When debugging a Java program on a console environment, outputting a paragraph of text to the console or text file is the simplest way to view the Program's running state, but this does not solve all of the Problems. Sometimes a good log system is necessary for a system where we cannot see the system output in real time or a system that does need to retain our output Information. therefore, can not arbitrarily output a variety of non-standard debugging information, these random output information is not controllable, difficult to clear, may be background monitoring, error removal and error recovery brings 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 during debugging, so that you can quickly navigate to the code where the log event Occurred.
Logs are divided into different levels according to a standard: post-graded logs that can be used for log filtering under the same Classification.
Support Multithreading: The log system is often used in multithreaded environments, especially in Java systems, so as a system resource, the log system should be guaranteed to be thread-safe.
Support for different recording media: different engineering projects often require different recording media requirements, so the log system must provide the necessary development interface to ensure that it is easier to replace the recording medium.
High performance: The log system usually provides high-speed logging capability to cope with the normal operation of the system under large request traffic under large System.
Stability: The log system must remain highly stable and cannot cause major business code crashes due to internal errors in the log System.
1.3 General Log System Introduction
In the Java world, the following three kinds of 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 concept often discourage users.
2) Jdk1.4loggingframework
Following log4j, the JDK Standards Board absorbed Log4j's basic ideas into the jdk, releasing the first log framework interface in JDK1.4 and providing a simple implementation.
3) Commonsloggingframwork
The framework is also the Apache Foundation project, which has been designed to enable Java projects to switch freely on the use of log4j and jdk1.4lloggingframework, so the framework provides a unified calling 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 adopts some log4j interfaces and concepts, but the internal implementation is completely different. Using Java to implement the log framework, the key technology lies in the internal implementation of the log framework features mentioned earlier, in Particular: the classification and level of logs, the design of the log distribution framework, the design of the logger and the high performance and high stability considerations in the DESIGN.
2.1 System Architecture
The log system framework can be divided into two parts: log recording module and log output Module. The logging module is responsible for creating and managing the logger (Logger), and each Logger object is responsible for receiving various log objects (logitem) that log information at different levels (loggerlevel), and the Logger object first gets all the logs that need to be logged. And dispatches the log to the log output module synchronously. The log output module is responsible for the creation and management of the log output (Appender), as well as the output of the log. Several different log outputs are allowed in the system, and the log output is responsible for logging to the storage medium. The system structure is shown in 1:
2 The architecture of the log system is given using UML class Diagrams:
In the architecture given in figure 2, the logger logger is the user interface for the entire log system framework, where programmers can log logs, and in order to classify the logs, the system design allows for multiple logger objects, each logger responsible for a log of a class of logs, 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. The logger object, when it receives a log message created and sent by the client, wraps the log message as a log object used internally by the log system logitem, in addition to the messages sent by the sending side, such as the Send-side class name, the sending event, the sending method name, the sending line number, and so On. These additional messages are very valuable for system tracking and DEBUGGING. The packaged Logitem is 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 is managed by the appendermanager, usually through the configuration file can be easily extended out of multiple Output.
2.2 Design of the log recording section
As mentioned earlier, the logging section is responsible for receiving log messages sent by the log system client, and managing the log Objects. The following is a detailed description of the design essentials of the logging section:
1. Management of the Logger
The system classifies logging by keeping multiple logger objects in a 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.Logger Logger Logger=logger.getlogger ("loggername");
In general, using the class name as the name of the logger makes the benefit of minimizing the conflict between the logger names (because Java classes use the package Name) and the ability to classify the log records as finely as Possible. therefore, Assuming that there is a usermanager class that needs to use the log service, the more general way to use it is:
2.Logger Logger Logger=logger.getlogger (usermanager.class);
2. Implementation of log grading
Depending on the purpose of the log, the level of logging is divided from low to high by five levels:
Debug-indicates that the output log is a debug message;
Info-indicates that the output log is a system prompt;
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 that caused 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, you can use the Logger class interface to directly invoke and output these levels of logs, which are described in logger:
3.public void Debug (String msg);//output debugging Information
4.public void info (String msg);//output system hint
5.public void warn (String msg);//output warning message
6.public void fatal (String Msg);//output system error
7.public void error (String msg);//output critical
By invoking these interfaces on the logger object, A level attribute is directly assigned to the log information, which lays the groundwork for subsequent work to output at different levels.
3. Access to 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 you can throw and catch an exception in the method, the stack of system calls has been automatically populated by the JVM in the caught exception object, and in JDK1.4 you can use Java.lang.StackTraceElement to get the basic information for each stack item, by logging client End output Log method calls the number of layers of the calculation, it can be relatively easy to get to the stacktraceelement object, to get to the output log when the Java class, class member methods, the line number and other Information. 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 Design of the Log output section
The design of the log output section is difficult, in the log system designed in this paper, the output of the log, multi-threaded support, the expansion of the log system, the efficiency of the log system and other issues are left to the log output part of the Management.
1. Inheritance structure of the log output
In the output section of the log is a two-tier structure that defines an abstract log output (abstractloggerappender) and then inherits the actual log output from the abstract class. Abstractloggerappender defines a series of methods for filtering logs, and the specific output to the storage medium is an abstract method, implemented by Subclasses. The console output and file output are implemented by default in the system, where the implementation of the console output is quite simple.
2. Internal implementation of the file output device
In the implementation of the logging section, the problem of multithreading and high efficiency is not considered, so the file output must take into account the processing of these Problems. The use of java.lang.Vector within the file output defines a thread-safe, high-speed buffer in which all logs that are dispatched to the file output are placed directly into the Cache. At the same time, a worker thread is defined inside the file output, which is responsible for periodically saving the contents of the cache to a file, while the backup of log files can be done during the saving process. Because of the high-speed buffering structure, it is clear that the log Client's call is no longer a synchronous call, so it is no longer necessary to wait until the file operation to return, increasing the speed of system Calls. This principle is shown in 3:
2.4 Design Difficulties
With the above design, a high-performance log system framework with good scalability has a certain embryonic shape. Several difficult problems in the design process need further Reflection.
first, whether the entire system should adopt a completely asynchronous structure, through a similar message mechanism to send logs to the log system by the log Client. This approach can be used as another mode of operation of the log system framework, which is considered in subsequent Designs.
second, in the file output can be seen, although the current can be extended multiple log output, but the present abstract class provides only the filtering mechanism of the log, but not the caching mechanism provided, the current caching mechanism is placed in the file output, so in the future further design, The caching mechanism in the file output can be moved to the abstract class.
2.5 Design Patterns
In the design process, we pay particular attention to the use of several 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 mode (strategy). In addition, a large number of singleton modes (Singleton) are Used. The appropriate use of design patterns in the design can speed up the design progress and improve the design Quality.
3. Summary
This paper discusses the basic characteristics of log system, realizes the meaning, method and internal structure of log system, and gives a detailed design of log system based on Java PLATFORM. At the same time, it is pointed out that the log system will develop in the direction of service and Asynchrony. As a convenient tracking debugging, data recovery tool, should promote the use of the log system in the appropriate environment
Design and implementation of Java Log System framework