One of the configuration log4j methods under Struts2

Source: Internet
Author: User

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)


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.