1. Why use log4j
When developing a system, always System.out.println to print some information, but these statements in the system after the stability, will not function, into the garbage code. Log4j can solve this problem;
the process of 2.log4j
You can use log4j to replace the system output statements, when your system is stable, as long as you modify the configuration file, you can not display print information.
3. Let's start with a simple example and look back at the parameters
3.1 Create a new Log4jtest.java under C disk, as follows:
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
public class Log4jtest
{
Static log log = Logfactory.getlog (Log4jTest.class.getName ());
public static void Main (string[] args)
{
Log.info ("I am testing");
int x = 0;
Try
{
x = 5/x;
}
catch (Exception e)
{
Log.info (e);
Log.debug (e);
}
}
}
3.2 Copy jar packs to C disk (Log4j-1.2.15.jar and Commons-logging.jar)
Log4j-1.2.15.jar can go to http://logging.apache.org/log4j/to download;
Commons-logging.jar (Lib is automatically generated when you add a struts framework to the project);
3.3 Create a file log4j.properties in the C packing directory, the contents are as follows:
Log4j.rootlogger=debu g, A1, A2
Log4j.appender. A1=org.apache.log4j.consoleappender
Log4j.appender. a1.layout=org.apache.log4j.patternlayout
Log4j.appender. a1.layout.conversionpattern=%5p [%d{yyyy-mm-dd Hh:mm:ss,sss}] (%f:%l)-%m%n
Log4j.appender. A2=org.apache.log4j.rollingfileappender
Log4j.appender. A2. File=example.log
Log4j.appender. A2. Maxfilesize= 100KB
Log4j.appender. A2. Maxbackupindex=1
Log4j.appender. a2.layout=org.apache.log4j.patternlayout
Log4j.appender. a2.layout.conversionpattern=%p%t%c-%m%n
3.4 Into the CMD console, to the C-packing directory, execute the following command
Javac-classpath C:/log4j-1.2.15.jar;c:/commons-logging.jar Log4jtest.java Carriage return
Java-classpath.; C:/log4j-1.2.15.jar;c:/commons-logging.jar log4jtest Carriage return
The 3.5 console output reads as follows:
INFO [main] (LOG4JTEST.JAVA:12)-I'm testing
INFO [main] (LOG4JTEST.JAVA:20)-java.lang.ArithmeticException:/by zero
DEBUG [main] (log4jtest.java:21)-java.lang.ArithmeticException:/by zero
3.6 At this time you will see in the C Packing directory out of the Example.log file, the contents are as follows:
INFO Main Log4jtest-I'm testing
INFO main log4jtest-java.lang.arithmeticexception:/by zero
DEBUG main log4jtest-java.lang.arithmeticexception:/by zero
4. Configuration file parameters (the above configuration file is divided into three blocks)
4.1 First block: LOG4J.ROOTLOGGER=DEBUG,A1,A2
A) Log4j.rootlogger the first parameter DEBUG (this value (the message priority displayed) has a range of 6: All<debug<info<warn<error<fatal<off)
b in the example above, debug is set, then the Log.debug (), Log.info (), Log.warn (), which is written in my program. Level >=debug will show up;
c If you set info, then the Log.debug () written in my program will not be displayed, Log.info (), Log.warn () ... The content will show up;
D a1,a2 Two for Appender (that is, you can each of the names corresponding to a piece of content), the two parameters combined with the second, three chunks of content to see;
4.2 Second BLOCK: This piece is used to define several parameters of the appender of the A1
A) Log4j.appender.a1=org.apache.log4j.consoleappender (indicates the display method of the A1, with several values below)
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size)
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
b) Log4j.appender.a1.layout=org.apache.log4j.patternlayout (indicates the type of display information for A1)
Org.apache.log4j.HTMLLayout (layout in HTML tabular format)
Org.apache.log4j.PatternLayout (flexibility to specify layout patterns)
Org.apache.log4j.SimpleLayout (level and information string that contains log information)
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)
c) log4j.appender.a1.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n (indicates the format of the display information for the A1)
%p output priority, i.e. All,debug,info,warn,error,fatal,off;
%r output the number of milliseconds to output the log information from the application startup;
The class of the output of%c, which is usually the full name of the class in which it belongs;
%t output The name of the thread that generated the log event;
%n output a carriage return line break, Windows platform is "/r/n", UNIX platform is "n";
%d the date or time that the log point-in-time is exported, the default format is ISO8601, or the format can be specified thereafter, such as (Yyyy-mm-dd hh:mm:ss,sss);
%l the number of lines in the output code;
%l the number of rows below the method in the file in which the output code belongs;
%F the file name in the output code;
%m the number of lines in the output code;
Remember that the above arguments are case-sensitive;
4.3 Third block: Now should understand the third piece of content!
5. Some notes
5.1 If it is a project, put the Log4j.properties file under the engineering src directory |