Log4j learning notes

xiaoxiao2021-03-05  21

Introduction to log4j, most of them start from three components: Category, Appender and Layout.

First, introduce Category

Logger defines a method of writing a log. However, the charm of Logger is not limited to this. To understand Logger, you need to master the following three points:

1, Logger hierarchical structure

You can define multiple loggers, and different loggers are divided into a simple principle.

For example, category "com.foo" is a Parent, COM.FOO.BAR. "Java" is "java.util" Parent. Root (root) is located at the top of the inheritance level.

The method of obtaining a root level example is:

Logger loggerroot = logger. Getrootlogger ();

Get an instance method called com.foo is:

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

GetrootLogger and GetLogger are two static methods of Logger, respectively, to create an instance of Logger. Typically, use a static method getLogger to define a Logger in each class, so that the name of the Logger is equal to the global name of the class name. Just like the following code:

Import org.apache.log4j.logger;

Import org.apache.log4j.basicconfigurator;

Public class myapp {

// define a static logger variable so that it references the

// logger instance named "myapp".

Static logger logger = logger.getlogger (myapp.class);

Public static void main (String [] args) {

......

}

}

2, grading log output.

Log4j defines five levels from low to high, namely: Debug, Info, Warn, Error, Fatal.

If a log level is specified for a logger, then this Logger will shield the output of the current level.

E.g:

// Set the level of logger

Logger.SetLevel (Level.info);

// is not output

Logger.debug ("Starting Search for NEAREST GAS Station);

// Log will be output

Logger.info ("Located Nearest Gas Station);

Logger.warn ("EXITING GAS Station Search");

3. Based on the Logger hierarchical structure, the lower Logger automatically inherits the output level of the upper Logger.

There is a good example in the Log4j Concept Manual:

// 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

Logger.debug ("Starting Search for NEAREST GAS Station);

// The logger instance barlogger, named "com.foo.bar",

// Will inherit its level from the logger named

// "com.foo" thus, The Following Request is enabled

// Because info> = info.

Barlogger.info ("Located Nearest Gas Station);

// this request is disabled, Because Debug

Barlogger.debug ("EXITING GAS STATION SECH");

Finally, summarize the common method of Logger:

Package org.apache.log4j;

Public class logger {

// Creation & RetrievalMETHODS:

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);

}

Second, introduce appender

Appender defines the output target of the log. Log4j defines the following objectives:

l Console L Files (file) L GUI Components L Remote Socket Servers (Socket-based Server) L JMS (Java Information Service) L NT Event Loggers (Windows Event Log) L Remote Unix Syslog Daemons (Remote UNIX background log service).

A logger can have one or more appenders. Logger's appender can inherit from the inheritance level of the Logger.

Third, introduce Layout

The output format of the Layout log content. Layout is usually saved in the configuration file.

% M

The message specified in the output code

% P

Output priority, DEBUG, INFO, WARN, ERROR, FATAL

% R

Output self-application start to output the log information for milliseconds

% C

Output the category to which is usually the full name of the class

% T

Output thread name generated by the log event

% N

Output a carriage return, the Windows platform is "/ r / n", the UNIX platform is "/ N"% D

The date or time of the output log time point, the default format is ISO8601, or it can be specified in the format, such as:% D {YYY MMM DD HH: mm: SS, SSS}, Output: October 18, 2002 22: 10:28,921

% L

Output log events, including category names, threads that occur, and number of rows in code. Example: Testlog4.main (Testlog4.java: 10)

Fourth, log4j configuration file

Log4j supports both formats that support XML and Java Properties files, both from the content.

The following code performs fast and simple Log4j settings (not reading the configuration file). The result of the configuration is to join a consoleapore to root Logger, and the root root log level is Debug. The output will be formatted by PatternLayout in "% -4R [% T]% -5p% C% X-% M% N" mode.

BasicConfigurator.configure ();

Performing the following code will read the log4j setting from the configuration file, the result of the setting depends on the content of the configuration file.

PropertyConfigurator.configure (args [0]);

Performing the following code will read the log4j setting from the XML file, the result of the setting depends on the contents of the file.

Domconfigurator.configure (args [0]);

Five, pick up

Log4j can generate log files at a certain amount of time, such as backup a file a day.

Log4j can automatically generate new log files based on log file size, such as a new log file per 100k.

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

New Post(0)