7. NESTED DIAGNOSTIC CONTEXTS system in the real world does not have to deal with multiple client requests. In such a typical multi-threaded system, different threads will process different clients. Logging is particularly adaptable to debugging and tracking of this complex distributed application. A common way to distinguish the logging of each client is to instant a new independent Logger for each client. This leads to a large number of Logger, and the cost of management has also exceeded the logging itself. Uniquely identifying each log request is a lightweight technology. Neil Harrison describes this method in Pattern Languages of Program Design 3, Edited by R. Martin, D. Riehle, And F. Buschmann (Addison-Wesley, 1997). In order to Uniquely identifies each request, the user pushes context information into NDC (NESTED DIAGNOSTIC Context). Example NDC categories as follows: public class NDC {// Used when printing the diagnosticpublic static String get (); // Remove the top of the context from the NDC.public static String pop (); // Add diagnostic context for the current thread .public static void push (String Message); // Remove The Diagnostic Context for this thread.public static void transove () NDC is like a stack to manage each thread. Note that all the org.apache.log4j.ndc classes are static. Suppose the NDC output is turned on, and each time a log request is generated, the appropriate log4j component contains a complete NDC stack for the thread that will output log. This is done without user intervention, users are only responsible for positioning the correct information in NDC, inserting small PUSH and POP methods in the correct location in the code. Conversely, there is a great change in the PER-Client implementation method in the code. In order to demonstrate this, let's take a sedate to an anonymous client as an example. This servlet can establish NDC when starting initialization before each other code. Context information can be the name of the client host and other information in the request. Typical information includes cookies. Therefore, even if servlet provides services to multiple customers, logs are initialized, such as the same logger, can still be distinguished because each customer request will have different NDC stacks. In contrast this with the complexity of passing a freshly instantiated logger to all code exercised during the client's request. However, some strange procedures, such as web server logging logs of virtual hosts, not generally relying on the context of the virtual host, but also rely on the software component to issue a request. Recently, the release version of the log4j supports multi-layer tree structure. This enhancement allows each virtual host to handle it in its own logger. 8. Optimize a parameter that is often referenced by logging is cost. This is a reasonable concept, a moderate application may generate thousands of log requests. Many efforts are spent on measurement and debugging Logging optimization. Log4j requires rapid and elasticity: the speed is most important, and the flexibility is second. Users should pay attention to subsequent optimization suggestions.
1. When the log is disabled, the log is optimized. When the log is completely closed, a log request is equal to a method of calling plus an integer comparison time. This cost is typically between 5-50 nanoseconds on the 233 MHz Pentium II machine. However, method calls include hidden costs for parameters. For example, for Logger Cat, Logger.debug ("Entry Number:" I "IS" String.Valueof (entry [i])); causing constructing information parameters, for example, transforming integer I and Entry [i ] To a string, and connect the intermediate string, regardless of whether the information is output. The construction cost of this parameter may be high, it mainly determines the size of the parameters called the call. The cost of avoiding parameters should be as follows, if (Logger if the logger's debug is turned off) will not cause the spending of parameters. On the other hand, if the logger is debug, it will generate the cost of whether the Logger can be used. Once in Debugenable, it is Debug. This is nothing to do, because the ability to judge the log is only about 1% of the log actual time. In the log4j, log requests in the logger class .logger is a class, Logger is a class, Instead of an interface. This reduces the elastic cost of the method call. Of course, the user uses pretreatment or compile time technology to compile all log declarations. This will result in perfect implementation. However, because binary applications Does not include any log declaration, the log is not possible to open the binary. With my point of view, this bigger price is not worth it. 2. When the log status is enabled The log is optimized. This is an essential optimization of the Logger level. When the log status is open, log4j still needs the level of the request and the level of Logger. However, Logger may not be arranged; they will inherit from their Father In this way, Logger may need to search for its ancestor before inheriting. This is a serious effort to make the search as possible. For example, the child logger is only connected to its presented Father Logger. An example of the BasicConfigurator The logger named com.foo.bar is connected to root Logger, so the Logger COM and COM.foo do not exist. This will significantly improve the speed of execution, especially when parsing the Logger layer structure. Typical hierarchical resolution is three times the logger completely closed. 3. When the log information is output, the log is optimized. This is the main spending of the formatting and sending it to its output source. Here We have made efforts to make the formatted implementation as fast as possible. Like apnder. It is actually a typical cost of about 100-300 milliseconds. Details ORG.Apache.log4.Performance.Logging. Although log4j has a lot of characteristics, But its first design goal is still speed. Some log4j components have been overridden many times to improve performance. However, contributors often put forward new optimization. You should know that the implementation test with SimpleLayout The output of log4j is as fast as system.out.println. 9. Summary Log4j is a popular log package written in Java. One of them is in the same feature is in LO Inheritance concept in gger. Inheritance with Logger can be output at any intervals. This reduces the cost of volume and minimization log. Easy management is one of the advantages of the Log4j API. As long as the log definition is added to the code, they can be controlled by the configuration file. They can be selected and sent to different multiple output sources to select a good format.