log4j Configuration J2ee,j2se

Source: Internet
Author: User
Tags log4j

The first step is to download the log4j jar package and copy it to your system.

Step two, write log4j.properties

Step three, call,

Log4j.properties file content:

#建立mylog, it is the log generator that prints to the console

#org. Apache.log4j.ConsoleAppender indicates the destination is the console

Log4j.appender.mylog = Org.apache.log4j.ConsoleAppender

#target指定输出流

Log4j.appender.mylog.target = System.out

#org. apache.log4j.PatternLayout specify the log output format

log4j.appender.mylog.layout= Org.apache.log4j.PatternLayout

#ConversionPattern给出具体的打印方式

Log4j.appender.mylog.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n

#建立my2log the logger to print to the file.

#RollingFileAppender scrolling Print information to a file, the file is limited

#FileAppender file size is not limited

Log4j.appender.my2log = Org.apache.log4j.RollingFileAppender

#File Specify the way to print to that file.

Log4j.appender.my2log.File = D://logger//mylog.log

#MaxFileSize the size of the specified file

Log4j.appender.my2log.MaxFileSize = 50KB

#MaxBackupIndex The maximum number of backup files for a specified file

Log4j.appender.my2log.MaxBackupIndex = 1

#org. apache.log4j.PatternLayout specify the log output format

log4j.appender.my2log.layout= Org.apache.log4j.PatternLayout

#ConversionPattern给出具体的打印方式

Log4j.appender.my2log.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n

#rootLogger specifies that the logger used has mylog and My2log

#warn Specify the type of print information: info: Debug Info warn: Warning message error: Wrong message

#级别从底到高: Info-->warn-->error

#定义info can print all the information;

#定义warn can print warn and error, unable to print info

#定义error can print error, can't print info and warn

Log4j.rootlogger = Info,mylog,my2log

Called in J2SE:

public class Test {

public static void Main (string[] args) {

Logger log = Logger.getlogger (Test.class.getName ());

Propertyconfigurator.configure ("D://testlog4j//lg4j.properties (Physical Address of File)");

for (int i=0;i<100;i++) {

Log.warn ("斕 cavity");

Log.error ("fff");

Log.info ("123");

}

}

}

The web system requires a servlet to start log4j

The servlet is as follows:

Import java.io.IOException;

Import Javax.servlet.ServletContext;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.logging.impl.Log4JCategoryLog;

Import Org.apache.log4j.Logger;

Import Org.apache.log4j.PropertyConfigurator;

Import com.sun.org.apache.bcel.internal.verifier.exc.StaticCodeConstraintException;

public class Logprint extends HttpServlet {

@Override

protected void doget (HttpServletRequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {

}

@Override

protected void DoPost (HttpServletRequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {

}

@Override

public void Init () throws Servletexception {

ServletContext SCT = Getservletcontext ();

String prefix = Sct.getrealpath ("/");

String file = Getinitparameter ("log4j");

if (file!= null) {

Propertyconfigurator.configure (Prefix+file);

}

System.out.println ("LOG4J sucess!!!!!");

}

The Web.xml configuration is as follows: <servlet> <servlet-name>log</servlet-name> <servlet-class>cn.**.*.log4j. lo4jtest</servlet-class> <init-param> <param-name>log4j</param-name> <param-value> Web-inf/lg4j.properties (relative path in the system) </param-value> </init-param> <load-on-startup>1</ Load-on-startup> </servlet> Next is called in the class: public class Operate {private Logger Logger = Logger.getlogger ("Operat E ");     Public DatabaseConnection Getdbconn () {Logger.info ("OPERAT1: Created"); return dbconn; }

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.