LOG4J consists of three important components: the priority of log information, output destination of log information, and output format of log information. The priority of the log information is from high to Error, Warn, INFO, DEBUG, and is used to specify the importance of this log information. The output destination of log information specifies the log to print to the console or in the file; The format controls the display of log information.
1, define the configuration file
In fact, you can also use the profile at all, but configure the log4j environment in your code. However, using the profile will make your application more flexible.
Log4j supports two profile formats, one is a Java feature file (key = value), one is a file in XML format. Let's introduce the method of using the log4j configuration file:
Configure root Logger, whose syntax is:
Log4j.rootlogger = [level], appendername, appendername, ... where Level is the priority of log records, divided into OFF, Fatal, Error, Warn, INFO, DEBUG, ALL, or your defined level. Log4J is recommended to use only four levels, priority from high to low, Error, Warn, Info, Debug. By the level defined here, you can control the switch to the corresponding level of log information in the application. For example, the INFO level is defined here, and all the DEBUG level log information in the application will not be printed.
Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time.
Configure the log information output Destination Appender, its syntax is
Log4j.Appender.Appendername = flly.qualified.name.of.Appender.class
Log4j.appender.Appendername.Option1 = value1
...
Log4j.Appender.Appendername.Optionn = VALUEN
Among them, the appender provided by Log4j has the following:
Org.apache.log4j.consoleAppender (console)
Org.apache.log4j.fileappender (file)
Org.apache.log4j.dailyrollingFileAppender (a log file is generated)
Org.apache.log4j.rollingfileAppender generated a new file when the file is reached in the specified size
Org.apache.log4j.writerappender (send log information to any specified place in stream format)
Configure the format (layout) of log information, whose syntax is:
Log4j.Appender.Appendername.Layout = Fully.qualified.Name.Of.Layout.class
Log4j.Appender.Appendername.Layout.Option1 = Value1
...
Log4j.Appender.Appendername.Layout.Optionn = VALUEN
Among them, Layout provided by LOG4J has the following:
Org.apache.log4j.htmlLayout (layout in HTML form)
Org.apache.log4j.patternlayout (you can flexibly designate layout mode)
Org.apache.log4j.sImplelayout (level and information string containing log information) org.apache.log4j.tcccLayout (including information, thread, category, etc.)
2, use log4j in the code
The following will describe how Log4j is used in the program code.
i, get the logger using log4j, the first step is to get the log recorder, which will be responsible for control log information. Its syntax is: public static logger getlogger (String name), obtains a logger by the specified name, and if necessary, create a new recorder for this name. Name generally takes the name of this class, such as: static logger logger = logger.getlogger (ServerWithlog4j.class.getname ());
II, reading a configuration file After obtaining the log logger, the second step will configure the log4j environment, its syntax: BasicConfigurator.configure (): Automatically quickly uses the default Log4j environment. PropertyConfigurator.configure (String configFileName): Read the configuration file written by using the characteristic file using Java. Domconfigurator.configure (String FileName): Read the configuration file in the XML form.
III, inserting record information (format log information) When the top two necessary steps are performed, you can easily use different priority logging statements into any place you want to log log, whose syntax is as follows:
Logger.debug (Object Message);
Logger.info (Object Message);
Logger.warn (Object Message);
Logger. Error (Object Message);
IV, Code Example Note: Blue - File Name Red - Place with Log4j
Hello.java
Import org.apache.log4j. *;
Public class hello {
Static logger logger = logger.getlogger (Hello.class);
Public static void main (String Argv []) {
PropertyConfigurator.configure ("Hello.Properties");
Logger.debug ("Hello World.");
Logger.info ("What a beatiful day.");
Try {
// If there is an abnormality here
Throw new nullpointerserException ();
} catch (exception e) {
Logger.warn ("NullpointersRexception", E);
}
}
}
Log4j.properties
Log4j.rootlogger = Debug, Stdout, R
Log4j.appender.stdout = org.apache.log4j.consoleAppender
Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout
# Pattern to output the caller's file name and line number.
Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% m% n # log4j.Appender.r = org.apache.log4j.rollingfileAppender
Log4j.Appender.r.file = example.log
Log4j.Appender.r.maxfilesize = 100kb
# Keep One Backup File
Log4j.Appender.r.maxbackupindex = 1
Log4j.Appender.r.Layout = org.apache.log4j.patternlayout
Log4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N
Log4j.xml
XML Version = "1.0" encoding = "UTF-8"?>
layout>
appender>
layout>
appender>
root>
log4j: Configuration>