1. What is log4j?
Log4j can help debug (sometimes debug is not useful) and analysis
2, the concept of log4j
There are three main components in log4j, which are Logger, Appender, and layout,log4j that allow developers to define multiple Logger, each with its own name and Logger to indicate affiliation by name. There is a Logger called root, which is always present and cannot be retrieved or referenced by name, and can be obtained through the Logger.getrootlogger () method, and the other Logger through the Logger.getlogger (String name) method.
Appender is used to indicate where all the log information is stored, LOG4J supports a variety of appender, such as console, files, GUI components, NT Event loggers, etc. A logger can have multiple appender, that is, you can output the log information to the screen, and also stored in a file.
The function of layout is to control the output of log information, that is, formatted output information.
The log information that will be output in log4j defines 5 levels, in turn, Debug, info, WARN, error, and fatal, and when output, only the level of information specified in the level above configuration can be true output, so it is convenient to configure the content to be output under different circumstances, Without the need to change the code, this is really convenient.
3. log4j configuration file
Although it is possible to implement the configuration in the program without the configuration file, this method is obviously undesirable in today's system development, and the configuration file must be used where the configuration files can be used. LOG4J supports two types of configuration files: XML format and Java property format, I prefer the latter, first look at a simple example, as follows:
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
First, the root is set, the format is Log4j.rootlogger=[level],appendername, ..., and the level is the set of levels that require output information, followed by the destination of the Appender output, Appendername is where you specify where the log information is exported. You can specify multiple output destinations at the same time. Configure the log information output destination Appender, whose syntax is
Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
Log4j.appender.appenderName.option1 = value1
...
Log4j.appender.appenderName.option = Valuen
LOG4J offers the following types of Appender:
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (creates new files when the file size reaches the specified size)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
Configure the format (layout) of the log information with the following syntax:
Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
Log4j.appender.appenderName.layout.option1 = value1
....
Log4j.appender.appenderName.layout.option = Valuen
LOG4J offers the following types of layout:
Org.apache.log4j.HTMLLayout (Layout in HTML table Form),
Org.apache.log4j.PatternLayout (flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log
log4j formats the log information in a print format similar to the printf function in C, with the following printing parameters:%m The message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds that the log information is consumed from the application boot to output
%c output belongs to the class, which is usually the full name of the class
%t output The name of the thread that generated the log event
%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
4, the use of log4j in the program
To use log4j in your own programs, you first need to import Commons-logging.jar and Logging-log4j-1.2.9.jar into the build path. The log4j.properties is then placed in the SRC root directory. This allows the log4j to be used in the program. Using log4j in a class, first declare a static variable Logger logger=logger.getlog ("classname"); Now you can use it as follows: Logger.debug ("Debug Message" or logger.info ("info message"), look at one of the following small examples:
1 import Com.foo.Bar;2 import Org.apache.log4j.Logger;3 import Org.apache.log4j.PropertyConfigurator;4 Public classMyApp {5 StaticLogger Logger = Logger.getlogger (MyApp.class. GetName ());6 Public Static voidMain (string[] args) {7 //Basicconfigurator replaced with Propertyconfigurator.8Propertyconfigurator.configure (args[0]);9Logger.info ("Entering application.");TenBar bar =NewBar (); One Bar.doit (); ALogger.info ("Exiting application."); - } -}
How to use log4j