2, the concept of Log4j
Log4j has three main components, which are Logger, Appender and Layout, Log4j allows developers to define multiple Logger, Logger each with its own name, to indicate the affiliation between Logger by name. A Logger called Root, it is always present, and can not be retrieved by name or reference, may be obtained by invoking the Logger.getRootLogger method (), the other by Logger Logger.getLogger (String name) method.
Appender is used to indicate to store all the log information to any place in Log4j supports multiple appender, such as console, files, GUI components, NT Event Loggers , etc., a Logger can have multiple Appender, that is, you can either Log information output to the screen, and stored in a file.
Layout Log role is to control the output of information, i.e. information formatted output.
Log information will be output Log4j defines five levels, followed by DEBUG, INFO, WARN, ERROR, and FATAL, when the output, only level higher than the level of information to real output specified configuration, so it is convenient configured to output the content under different circumstances, without the need to change the code, this is really easy ah.
3, Log4j configuration file
, although you can not profile configuration is achieved in the program, but this method of systems development today is clearly undesirable, can adopt the configuration file must be sure to use local configuration file. Log4j supports two configuration file format: 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
# Output to the Pattern The Caller 'and Line Number apos File name.
log4j.appender.stdout.layout.ConversionPattern% 5P = [% T ](% F.:% L) -% m% n-
log4j.appender. R & lt = org.apache.log4j.RollingFileAppender
log4j.appender.R.File = example.log
log4j.appender.R.MaxFileSize = 100KB
# the Keep One Backup File
=. 1 log4j.appender.R.MaxBackupIndex
line log4j.appender.R.layout = Org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern% P% T =% C -% m% n-
first, is set the root the format log4j.rootLogger = [level], appenderName, ..., which is the level required to set the output level information, followed by the destination of the output of the appender, appenderName is to specify the output log information to which place. You can specify multiple output destinations. Configuration information log output destination Appender, the syntax is
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = VALUE1
...
= valueN log4j.appender.appenderName.option
the appender Log4j provided are the following:
org.apache.log4j.ConsoleAppender tells that (console)
org.apache.log4j.FileAppender (file)
org.apache.log4j.DailyRollingFileAppender generates a (per day log file)
org.apache.log4j.RollingFileAppender (file size reaches the specified size to generate a new file)
Org.apache.log4j.WriterAppender (log information to any designated place in stream format)
log information format (layout ), the syntax is:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1
....
log4j.appender.appenderName.layout.option = valueN
layout Log4j provided are the following:
Org.apache.log4j.HTMLLayout (HTML table format layout),
Org.apache.log4j.PatternLayout (the flexibility to specify the layout mode),
Org.apache.log4j.SimpleLayout (including log information and level information and character string),
Org.apache.log4j.TTCCLayout (log time comprising the time, thread, type information, etc.)
the Log4J similar C language printf function Print log information formatted for printing parameters are as follows:% m output code specified in the message <XML:? NAMESPACE PREFIX = O />
% p output priority, i.e. the DEBUG, the INFO, WARN, ERROR, FATAL
% R & lt output since the number of milliseconds to output the log information spent
% c output category belongs, usually the full name of the class where
% t produce output the thread name of the log events
output% n Enter a newline, Windows platform "\ r \ n", Unix platform "\ n"
date or time% d output log point in time, the default format is ISO8601, you can specified in the following format, for example:% d {yyy MMM dd HH : mm: ss, SSS}, output similar to: <the XML:? the NAMESPACE the PREFIX = ST1 /> 2002 Day 18 is dated 10 22 is: 10: 28,921
% occurrence position l output log events, including categories were the threads, as well as the number of lines in the code. Example: Testlog4.main (TestLog4.java:10)
. 4, Log4j used in a program to be used in their Log4j routine, first need to import commons-logging.jar and logging-log4j-1.2.9.jar to build path. Then log4j.properties into the src root directory. So that you can use log4j in the program. Use in class log4j, first declare a variable static Logger logger = Logger.getLog ( "classname" ); Now you can use, and is used as follows: logger.debug ( "debug message") or logger.info ( "info message" ), a small look at the following example: Import com.foo.Bar; Import the org.apache.log4j.Logger; Import org.apache.log4j.PropertyConfigurator; public class the MyApp { static Logger Logger = Logger.getLogger (MyApp.class.getName ()); public static void main (String [] args) { . // the BasicConfigurator REPLACED with the PropertyConfigurator PropertyConfigurator.configure (args [0]); logger.info ( "Entering file application.");
Bar bar = new Bar();
bar.doIt();
logger.info("Exiting application.");
}
}
Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/08/23/1274568.html