LOG4J Concise Manual 1. Overview This article mainly describes the unique characteristics of the LOG4J API and its design principle. Log4j is a project based on many authors. It allows the developer to control the output of the log in any interval. It achieves flexible settings at runtime by setting a configuration file outside. Most importantly, log4j has a smooth learning curve. Note: Depending on the feedback from the user, it is easy to add addiction. 2. Introduction Almost all large applications include its own logs and tracking APIs. Adapt to this rule, E.U. Semper project determines writing its own tracking PAI. This is early 1996. After countless times, after several deformations and many work, the API has become a popular Java log package today. This package is published in Apache Software License protocol, a mature open source. The latest log4j version, including all source code, Class files, and documents, you can find it at http://jakarta.apache.org/log4j/. Incidentally, log4j has provided an interface to C, C , C #, Python, Ruby, and Eiffel language. In order to debug, the insertion log output is a method of low-tech ingredients, but it may be a unique method because the debugger is not always available or can be adapted, especially for multi-threaded distributions . Experience stated that debugging is an important part of the software development cycle. Log4j has several advantages: First, it provides an accurate environment for running procedures. Once the code is inserted, debugging information can be generated without manual intervention. Second, the log output can be preserved in a permanent medium for future research. In addition, in addition to the development cycle, a full detailed log package can be used as a subsequent statistical tool. Log4j is of course its shortcomings, it may slow down the program. If it is too detailed, it may cause the screen blindly scrolling. These situations are excluded, and log4j is reliable, fast, can be expandable. Because the logs are rarely the main purpose of an application, designers are working hard to make log4j API learning and use simplification. 3. Log class, output source, and layout log4j have three main components: log category, an output source (Appenders), and layout (Layouts). These three types of components work together such that the developer can record them according to the type and level of the information, and control the output format and location of this information at runtime. 3.1 Logs classics (loggers) log4j The primary relative to simple use of the System.Println () method is based on its ability to do not prevent other information while disabling some particular information output. This capability is derived from log namespace, that is, all log declarations, it is classified according to the formulas selected by some developers. Previous observation guides us to choose categories as the center concept of the package. However, since the LOG4J version 1.2, the Logger class is replaced by the Catalog class. For those who are familiar with Log4j, the Logger class can be imagined to be just a category class alias. Loggers is specified as entity, logger's name is sensitive, they follow the following naming rules: 2 Name Inheritance If the name of the category is the prefix of its subcategory name, it is another category Ancestors. If there is no other inheritance relationship between a category (Logger) and its subcategory, we call the relationship between PARENT and Child. For example, category "com.foo" is a Parent, COM.FOO.BAR.
Similarly, "java" is "java.util" Parent, is the father of "java.util.Vector". This naming rule should be familiar with most developers. The root category is located at the top of the Logger inherited structure. It has two exceptions: 1. It has been present 2. It cannot be obtained according to the name. The static method of calling classes logger.getrootlogger can get it. All other Loggers can get their own instances by static methods logger.getlogger. This method takes the hope of the Logger name as a parameter. Some basic examples of methods Logger follows: package org.apache.Log4j; public Logger class {// Creation & retrieval methods: public static Logger getRootLogger (); public static Logger getLogger (String name); // printing methods: public void debug (Object message); public void info (Object message); public void warn (Object message); public void error (Object message); // generic printing method: public void log (Level l, Object message);} Loggers can Assigned level. All levels of all levels include: DebuginFowarnerrorfatal they are defined in org.apache.log4j.LOVEL classes. Although we don't encourage, you can define your own level by inheriting the Level class. We will then introduce a better way. If a logger is not assigned a level, it will inherit from an allocated ANCESTOR that is allocated level. Regularly said: 2 Inherits For a given Logger C, its inheritance level is equal to the first level of Logger with non-empty levels from C. To ensure that all Loggers can eventually be inherited to a level, root Logger usually has a level that has been defined. The data in the following four tables demonstrate the results obtained according to the above rules. The level of the class allocated RootProotProotX NoneProotX.y NoneProotX.y.znoneProTexample 1 In Example 1, only root Logger defines a level, its level value - "Proot" is all other loggers x, xy, And inherited with XYZ. The level of inheritance of the category name allocation RootProotProotX PXPXX.Y PXYPXYX.Y.ZPXYZPXYZEXAMPLE 2 In Example 2, all Logge has a assigned level value, so they do not need levels. The level of inheritance of the category name allocation RootProotProotx PxPxx.y nonepxx.y.zpxyzpxyzexample 3 In Example 3, root Logger, and X and X.y.z are assigned a level proot, PX and PXYZ. Logger x.y inherits the level value PX from its Parent X. The level of the class name allocated RootProotProotx Pxpxx.y Nonepxx.y.znonepxexample 4 In Example 4, roots Logger and X are assigned a level "proot" and "PX", Logger XY and XYZ are allocated levels It is close to their ancestor x to get inheritance. We need to implement a log request by calling one of the example methods of the logger.
These output methods are DEBUG, INFO, WARN, ERROR, Fatal, and Log. Levels to distinguish the request of the log by defining the output method. For example, if c is an instance of a Logger, then declares that C.info is an INFO level log request. If the level of the request of a log is higher than or equal to the level of the log, it can be enabled. Conversely, it will be disabled. A Logger that is not arranged will be inherited from its parents. This rule summarizes the following. 2 Basic selection rules If one level is a log request in the Logger at a level q, if P> = Q, the request will be enabled. This is the core principle of log4j. It assumes that level is in an orderly. For standard levels, we define Debug We can define a logger in each class with a static initialization method of Logger, so that the name of the Logger is equal to the global name of the class name, and the logger is named. This is an effective simple definition of a Logger method. Because the log output has the name of the class that generates the log, this naming policy makes us easier to locate the source of a log information. Although ordinary, it is one of the common strategies of naming Logger. Log4j does not limit the possibility of defining the Logger. Developers can definite the name of the Logger in accordance with their wishes. However, named Logger is the best way to be currently known as the location of the class. 3.2 Appenders and Layouts Selected or Disable Log Request is only part of the log4j. The log4j allows the log request to be output to multiple output sources. With log4j, an output source is called a appender. Appender includes Console (Console), Files (Files), Remote Socket Servers, JMS (Java Information Services), NT Event Loggers, And Remote Unix Syslog Daemons (Background Log Service for Remote Unix). It can also be asynchronous records. A Logger can set more than one appender. Add an APPENDER to a given Logger with the AddAppender method. For a given logger, each of its effective log requests are forwarded to the Logger all Appender on the appender of the Logger's Logger. In other words, appender automatically enables inheritance from its parent. For example, if a root Logger has a Console Appender, then all valid log requests will be at least on the Console. If a Logger called C has an Appender of a file type, it will take effect on itself and all its sub-loggers. We can also overload the default behavior of the appender by setting the Appender's Additive Flag to False, so that the attributes of the inheritance are not taken effect. The rules for adjusting the output source (appender) are as follows. Output source Additive (appender additivity) A log definition of log definition named C will continue to its own and its ANCESTOR Logger's appenders. This is the meaning of the term "appender additivity". However, a Annestor Logger P of Logger C, which is set to false, then C's output will be positioned to all the Appender of all C, and all Appender from it from it to P. The attached tag (Additive Flag) is default to True. The following table is an example.