Log4j It consists of three important components: log information priority, log information output destination, and log information output format. The priority of log information ranges from high to low. Error , Warn , Info , Debug To specify the importance of the log information. The log output destination specifies whether the log will be printed to the console or a file. The output format controls the display of the log information.
1, Define the configuration file
In fact, you can not use the configuration file, but inCodeConfiguringLog4jEnvironment. However, using the configuration file will make your applicationProgramMore flexible.
Log4jTwo configuration file formats are supported.JavaFeature file (Key=Value ).XMLFormat File. The following describes how to useLog4jConfiguration file method:
Configuration RootLoggerThe syntax is:
Log4j. rootlogger = [level], appendername, appendername ,...
Where,LevelIs the log record priority, dividedOff,Fatal,Error,Warn,Info,Debug,AllOr the level you define.Log4jWe recommend that you use only four levels. The priority ranges from high to low.Error,Warn,Info,Debug. By defining the level here, you can control the switch to the corresponding level of log information in the application. For exampleInfoLevel, allDebugThe log information is not printed.
AppendernameSpecifies where the log information is output. You can specify multiple output destinations at the same time.
Configure the log output destinationAppenderThe syntax is
Log4j. appender. appendername = fully. Qualified. Name. Of. appender. Class
Log4j. appender. appendername. option1 = value1
...
Log4j. appender. appendername. optionn = valuen
Where,Log4jProvidedAppenderThere are the following types:
Org. Apache. log4j. leleappender(Console)
Org. Apache. log4j. fileappender(File)
Org. Apache. log4j. dailyrollingfileappender(One log file is generated every day)
Org. Apache. log4j. rollingfileappender(A new file is generated when the file size reaches the specified size)
Org. Apache. log4j. writerappender(Send log information to any specified place in stream format)
Configure the log information format (layout). Its syntax is:
Log4j. appender. appendername. layout = fully. Qualified. Name. Of. layout. Class
Log4j. appender. appendername. layout. option1 = value1
...
Log4j. appender. appendername. layout. optionn = valuen
Where,Log4jProvidedLayoutThere are the following types:
Org. Apache. log4j. htmllayout(HtmlTable layout)
Org. Apache. log4j. patternlayout(You can flexibly specify the layout mode)
Org. Apache. log4j. simplelayout(The level and string containing the log information)
Org. Apache. log4j. ttcclayout(Including the log generation time, thread, category, and so on)
2, Used in codeLog4j
The following describes how to useLog4j.
IAnd obtain the recorder UseLog4jThe first step is to obtain the logger, which controls the log information. Its syntax is:Public static logger getlogger (string name)Obtain the recorder by the specified name. If necessary, create a new recorder for the name.NameThis class name is generally used, for example:Static logger = logger. getlogger (serverwithlog4j. Class. getname ());
IIRead the configuration file. After obtaining the logger, configureLog4jEnvironment. Its syntax is:Basicconfigurator. Configure (): Automatically and quickly use the defaultLog4jEnvironment.Propertyconfigurator. Configure (string configfilename): Read and useJava.Domconfigurator. Configure (string filename): ReadXMLConfiguration file.
IIIInsert record information (format log information) when the two necessary steps are completed, you can easily use the log record statements of different priorities to insert to any location where you want to record the log, the syntax is as follows:
Logger. debug (Object message );
Logger.info (Object message );
Logger. Warn (Object message );
Logger. Error (Object message );
IVCode example Note: Blue - File Name Red - UsedLog4jLocation
Hello. Java
Import org. Apache. log4j .*;
Public class Hello {
Static 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 an exception occurs
Throw new nullpointerexception ();
} Catch (exception e ){
Logger. Warn ("nullpointerexception", e );
}
}
}
Log4j. Properties
Log4j. rootlogger = debug, stdout, R
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
# Pattern to output the caller's file name and line number.
Log4j. appender. stdout. layout. conversionpattern = % 5 p [% 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"?>
<! Doctype log4j: Configuration System "log4j. DTD">
<Log4j: configuration xmlns: log4j = "http://jakarta.apache.org/log4j/">
<! -- ===================================================== =========-->
<! ---->
<! --Rolling file appender-->
<! -- ===================================================== =========-->
<Appender name = "B1" class = "org. Apache. log4j. rollingfileappender">
<Param name = "file" value = "log.htm"/>
<Param name = "APPEND" value = "false"/>
<Layout class = "org. Apache. log4j. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] % P-% m % N"/>
</Layout>
</Appender>
<! -- ===================================================== =========-->
<! ---->
<! --Console appender -->
<! -- ===================================================== =========-->
<Appender name = "A1" class = "org. Apache. log4j. leleappender">
<Layout class = "org. Apache. log4j. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] % P-% m % N"/>
</Layout>
</Appender>
<! -- Define root -->
<Root>
<Level value = "info"/>
<Appender-ref = "A1"/>
<Appender-ref = "B1"/>
</Root>
</Log4j: configuration>
Download:/files/Jenry/log4j.rar