This method is configured in conjunction with LOG4J.
First, the libraries required to import log4j
Second, configure an initially loaded servlet in Web. Xml.
<servlet>
<servlet-name>Boottrap</servlet-name>
<servlet-class> cn.com.aliex.boottrapservlet</servlet-class>
<init-param>
<param-name>xxx</ param-name>
<param-value>123</param-value>
</init-param>
<load-on-startup >1</load-on-startup>
</servlet>
Third, the configuration log4j.properties into the project directory.
Log4j.rootlogger=error
Log4j.logger.cn.com.aliex=debug,aliex
log4j.appender.aliex= Org.apache.log4j.RollingFileAppender
log4j.appender.aliex.threshold=debug
log4j.appender.aliex.file=${ Webapproot}\\log\\log.log
log4j.appender.aliex.maxfilesize=8000kb
log4j.appender.aliex.layout= Org.apache.log4j.PatternLayout
log4j.appender.aliex.layout.conversionpattern=[%d] [%t] [%-5p]%c-%m%n
Iv. initializing log4j in the servlet that initiates the load
String Currentdir = Getservletcontext (). Getrealpath ("");
This setting is used to generate directory System.setproperty for log files loaded in log4j
("Webapproot", Currentdir);
System.out.println ("Currentdir---" + currentdir);
SYSTEM.OUT.PRINTLN ("System uses JDK version--" + System.getproperty ("java.version"));
Load the log4j configuration file
try {
ClassLoader loader = Thread.CurrentThread (). Getcontextclassloader ();
Properties log4j = new properties ();
Log4j.load (Loader.getresourceasstream ("conf/log4j.properties"));
Propertyconfigurator.configure (log4j);
} catch (IOException e) {
e.printstacktrace ();
}
The code can be placed in the init () method.
Directory Structure (MYECLIPSE)