You can download from the address below to log4j, current version: 1.3 http://logging.apache.org/site/binIndex.html log4j No installation, you only need to put log4j.jar in classpath you can use.
Or Copy log4j.jar to ClassPath, in Tomcat you can put it in the tomcat_home / common / lib directory or Copy log4j.jar to Web-INF / LIB 1, introduce log4j including three important components: loggers, Appenders, Layout, is used to represent recorders, output destinations, and control print formats. Some logger classes are described as follows: public class logger {// creeation & retrieval methods: public static logger getRootlogger (); public static logger getLogger (String name); // printing methods: public void debug (Object message); public void info (Object message); public void warn (Object message); public void error (Object message); public void fatal (Object message); // Generic Printing Method: Public Void Log (Level L, Object Message);} 2, Defining Profiles: 1) Configure Log 4j.Rootlogger = [Level], appendername, appendername, ... Eg: log4j.rootlogger = Warn, A1 , R log4j.logger.dao = warn, A2 (where DAO is your own defined) Recorder is given Level (Level), here there is a predetermined level of standard: Debug, Info, Warn, Error, Fatal (level is low High) 2) Configure the log information Output Destination Appender, whose syntax is log4j.qumenter.AppenderName = full.qualified.name.of .appender.class log4j.Appender.Appendername.Option1 = value1 Among them, the appender provided by log4j has the following: org.apache.log4j.consoleAppender, org.apache.log4j.fileappender (file), org.apache .log4j.dailyRollingFileAppender (a log file a day), org.apache.log4j.rollingFileAppender generates a new file when specified by specified size, org.apache.log4j.writeraPpender (send log information in stream format to Any designated place) Eg: log4j.Appender.r = org.apache.log4j.rollingfileappender log4j.Appender.r.file =
C: /Log4j.log log4j.rapnder.r.maxfilesize = 100000kb log4j.Appender.r.maxbackupindex = 1 log4j.Appender.r.Layout = org.apache.log4j.ttccLayout 3) Configuring log information format (layout), Its syntax is: log4j.Appender.Appendername.Layout = Fully.qualified.Name.Of.Layout.class log4j.Appender.Appendername.Layout.Option1 = Value1 Among them, Layout provided by log4j has the following: org.apache.log4j .HtmlLayout (layout in HTML form), org.apache.log4j.patternlayout (can be flexibly specified), org.apache.log4j.simplelayout (level and information string containing log information), org.apache.log4j .TtccLayout (including information, thread, category, etc.) EG: log4j.appender.a2.Layout = org.apache.log4j.ttccLayout log4j.Appender.a2.Layout.conversionPattern =% P% T% C - % M% N3, using private static final logger = logger.getlogger (classname); // ClassName: Java file name log.debug ("........."); log. Error ("........."); ..... Specific use you can see the log4j document. 4. How to set the to different log files in different modules: The main method is to generate different recorders for different modules (ie, logger, the example is DAO, ACTION, TEST), and then set these Different recorders output logs to different appenders (A1, R, A2, A3, A4), thereby implementing this feature. 5. About format: Example 1: Display date and log information log4j.Appender.a2.Layout = org.apache.log4j.patternLayout log4j.Appender.a2.Layout.conversionPattern =% d {YYYY-mm-dd hh: mm: SS, SSS}% M% N printing information is: 2002-11-12 11: 49: 42, 866 Select * from tablename where 1 = 1 ORDER BY FIELD DESC
Example 2: Display Date, Log Information Local and Log Information Log4j.Appender.a2.Layout = Org.Apache.log4j.patternLayout log4j.Appender.a2.Layout.conversionPattern =% D {YYYY-MM-DD HH: mm: SS The information of SSS}% L "#"% M% N printing information is: 2002-11-12 11: 51: 46, 313 packagename.classname.select (classname.java: 409) "#" SELECT * from Tablename Where 1 = 1 Order by Field Desc Example 3: Display LOG Level, Time, Call Method, Log Information Log4j.Appender.a2.Layout = Org.Apache.log4j.patternLayout log4j.Appender.a2.Layout.conversionPattern = [% - 5P]% D {YYYY-MM-DD HH: MM: SS, SSS} Method:% L% N% M% N printing information is: [debug] 2002-11-12 12: 007, 376 Method: packagename.classname.select ( Classname.java: 409) Select * from tablename where 1 = 1 Order by Field Desc6, write startup file log4j's initialization has several ways, I only provide one way.
INITSERVLET.JAVA (a servlet) first way ================================== ========= Public class initservlet extends httpservlet {Public void init () throws servletexception {servletContext Sct = getServletContext (); system.out.println ("[log4j]: The root path:" Sct.getRealPath ("/")); system.out.println (" [Log4j]: INITSERVLET INIT START ... "); org.apache.log4j.propertyconfigurator.configure (Sct.getRealPath (" / ") GetServletConfig (). GetInitParameter (" log4j "); system.out.println "[Log4j]: initservlet init over.");} ...} second way =========================== ======== Public void init () throws servletexception {try {systemStarter.getInstance (). Init (getpath () "filename");} catch (processException ex) {EX.PrintStackTrace ();}} The second way =========================================== BasicConfigurator.configure (): Automatically use the default Log4j environment. Domconfigurator.configure (String FileName): Read the configuration file in the XML form.