Log4j learning notes (1)

zhaozj2021-02-16  70

Log4j learning notes (1)

To be honest, in addition to log4j's function, I prefer its logo. The following note is mainly "Borrow from" log4j's random document "Short Introduction to log4j", written by CEKI Gülcü in March 2002, other reference documents See it.

1, log4j has been ported to C, C , C #, Perl, Python, Ruby, Eiffel several languages. 2, log4j has three main components: recorder, storage device, layout 3, recorder (recorder can not care about log data storage) log4j allows programmers to define multiple recorders, each recorder has its own name By the name between the recorder, it indicates the affiliate relationship (or family relationship). Such as the recorder A.B, between the recording A.b.c is a parent child relationship, and the recorder A and A.b.c are the relationship between ancestors and future generations, and the father and child relationship is the special case of the ancestors and progeny. With this relationship, logical relationships between different recorders can be described. There is a recorder called the root recorder, which is always existed and cannot be retrieved or referenced by name, and it can be obtained through the logger.getrootlogger () method, and the general logger is passed through the Logger.getLogger method. Below is the basic method of the Logger class.

Package org.apache.log4j;

Public class logger {

// 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); public void fatal (Object message);

// generic printing method: public void log (level L, Object Message);

The recorder has an important attribute, which is the level. (This is a good understanding, just like a family, there is a generation of generations, but the height of different members may not be the same, and the height and generations have nothing to do) programmers can give different recorders to different levels, if A member is not explicit, and the nearest level value is automatically inherited. The root recorder has a total level value. E.g:

Level value inherited by the recorder name RootProotProotX PX PX X.Y None PX x.y.znone PX

Programmers can freely define levels. There is a prejudice relationship between the level values, such as the relationship with DEBUG, as several levels

Each log information to be output has a level value.

In the previous logger class, several levels of Debug, Info, Warn, Error, Fatal are predefined. Because of the method binding, people are easy to misunderstand, in fact, these methods only indicate the log information to be recorded. level. When the log () method is called, the level of log information needs to be explicitly specified by parameters.

If a log information is level, it is greater than the level value equal to the logger, then the logger will record it. If you feel difficult to understand, you can refer to the following example.

// Get a logger instance named "com.foo" logger logger = logger.getlogger ("com.foo");

// Now set its level. Normally you do not need to set the // level of a logger programmatically. This is usually done // in configuration files. Logger.Setlevel (Level.info);

Logger Barlogger = logger.getlogger ("com.foo.bar");

// this Request is enabled, Because Warn> = info. Logger.warn ("Low Fuel Level.");

// this Request is disabled, because debug

//// 's "com.foo.bar", // Will inherit its level from the logger named // "com.foo" thus, the folload request is enabled // BECAUSE INFO> = INFO. BARLOGGER. "" LOCATED NEAREST GAS Station. ");

// this request is disabled, Because Debug

There are a few interesting situations, one is when a recorder is instantiated, call getLogger () with the same name again, will return to it, which is very beneficial to use the same recorder in different code or classes LOG information, the other is that the ancestors in the nature are different from the descendants, and the ancestors of a recorder can appear late than the post-generation record, but will automatically establish such a family relationship according to the relationship between the name.

4. The storage device is output to the destination through the register. Supported registers include Console, Files, GUI Components, Remote Socket Servers, JMS, NT Event Loggers, Remote UNIX Syslog daemons. With the FILE storage, the log information can be output to a different file (ie, different destinations). LOG information can be stored asynchronously. A recorder can have multiple regulators that can increase the storage device by method addAppender. A blog information can be processed by this recorder, then the logger sent this information to the storage device that it it. Each recorder has a inheritance switch that determines whether the recorder is / not inherits the storage device of its parent logger. Note that if the inherit is only inherited, it is only inherited, regardless of the farther ancestor. Refer to the table below:

Memo recorder destination to increase storage inherits stored output rootA1not applicableA1The root logger is anonymous but can be accessed with the Logger.getRootLogger () method. There is no default appender attached to root.xA-x1, A-x2TRUEA1 , A-X1, A-X2Appenders of "x" and root.x.ynontruea1, a-x1, a-x2appenders of "x" and root.xyza-xyz1truea1, a-x1, a-x2, A-XYZ1APPENDERS IN "xyz", "x" and root.securityA-secFALSEA-secNo appender accumulation since the additivity flag is set to false.security.accessnoneTRUEA-secOnly appenders of "security" because the additivity flag in "security" is set to false.5 ,layout

The layout is responsible for formatting the LOG information of the output. Log4j's PatternLayout can define formats with formatting templates similar to C language Printf.

6, log4j can automatically provide some log information according to the standards developed by the programmer, which is helpful to the case where the class that needs frequent logs. The object's automatic log has inheritance.

转载请注明原文地址:https://www.9cbs.com/read-15041.html

New Post(0)