Use of log4j and common-logging

Source: Internet
Author: User
Tags log log log4j
 log4j and common-logging use log4j and common-logging are open source projects under Apache, their differences and simple use please refer to the following Web site. http://touch.javaeye.com/blog/31632 This is a very good article, I just want to give him a supplement.   Add one: you can see whether it's log4j or commons-logging, you need the following steps to use in your program: 1.       initialization, generating a static instance: Commons-logging is:  private   static  log log  =  . Logfactory.getlog (testlog. class );    log4j is: Static  logger logger  =  logger.getlogger (TestLog4j.   class );   propertyconfigurator.configure (  " log4j.properties "  );    the second sentence can be omitted, by default, he will find  log4j.properties himself. 2. Use in the program: Logger.debug (  " Here is some DEBUG "  );  Logger.info (  "  Here is some INFO  " );    Logger.warn (  " Here is some WARN "  );    If you do this, you will generate a static instance in each program, which is inappropriate if it is a larger project. We can in the entire applicationGenerates an instance, which is initialized once and uses this instance throughout the application. For example, we write a log initialization class, as follows: Package myb.hi.core.log;   Import org.apache.commons.logging.*;   public class Logs {       private static log log;      & nbsp /**         * Define A static Log variable,        & nbsp;*/                 static{                       try{                             Log=logfactory.getlog (Logs.class);                      }catch (Exception ex) {          &NBsp;                 System.out.println ("can" t init the Logger, caused by: "+ex);                      }              }               /**         * Get the log Object         * @return Log         */ & nbsp;     public static Log GetLogger () {               return log;       } The code above uses commons-logging to generate a static log instance, which can be done by later programs: Logs.getlogger (). info (" Begin Action:UserBaseInfoAction.getSingleUserInfo () "); Dbsession.begin (); String Fname=userform.getfname (); Userform=usErbaseinfobusiness.getsingleuserinfo (Dbsession.getsession (), fname); Dbsession.commit (); Request.setattribute ("UserInfo", UserForm); Logs.getlogger (). info ("End Action:UserBaseInfoAction.getSingleUserInfo ()");   Use Logs.getlogger () to obtain this log instance where records are needed, and then call his method (Info,debug,error, etc.).   LOG4J implementations are similar, initialization classes can be written like this: package myb.hi.core.log;   Import Org.apache.log4j.Logger; Import Org.apache.log4j.PropertyConfigurator;   public class Logs {       private static Logger log Logger;    &nbsp ;  /**         * Define A static Log variable,     & nbsp;   */                 static{                      try{                             Logger=logger.getlogger (LogInit.class);                                   // Domconfigurator.configure ("E:/study/log4j/log4j.xml");//load. xml file                           // Propertyconfigurator.configure ("log4j.properties")//load. properties File                     }catch (Exception ex) {                              System.out.println ("can" t init the Logger, caused by: "+ex);                     }               }              / * *         * get the Log object         * @return Log         */       public static Logger GetLogger () {              return logger    The calls in the     } application are exactly the same. Add two: Log4j.properties file, logger defined log level can be different from appender. When defining logger, try to write down the log level as much as possible, while defining the log level in Appender can be written as needed. For example, define a logger, Log4j.rootlogger = Debug, console,a1 defined as debug, no matter appender how to define, there must be output. If defined as: Log4j.rootlogger = INFO, console,a1 defined as Info,appender must have a higher level of definition than he higher, and if Appender is defined as debug, it will not output.   Similarly, when calling logger in a program, the calling method level must be higher than the level defined by logger to have output. For example, Log4j.rootlogger = INFO, CONSOLE,A1 is defined asinfo, in the program, Logs.getlogger (). The info () method can have output; Logs.getlogger (). The debug () method will not have any output.   Supplementary Three: about the log file output path. If you do not specify a path, the log file is output in the running directory, which is the application server's Bin directory, such as the following: Log4j.appender.A1.File = samplemessages.log4j assumes that the application server uses Tomcat, Then the samplemessages.log4j file will be in the bin subdirectory of the Tomcat installation directory. In order to store the log files uniformly, use absolute or relative paths, such as the following: Log4j.appender.A1.File =. /logs/samplemessages.log4j so that the log files will be placed in Tomcat's logs directory.  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.