Configuring log4j logs in the SSH framework and STRUTS2 configuration exception handling methods

Source: Internet
Author: User
Tags exception handling log4j
1. log4j.properties File Configuration Example
# Default properties to initialise log4j
log4j.rootlogger=error,file,a1

log4j.appender.a1= Org.apache.log4j.ConsoleAppender
log4j.appender.a1.layout=org.apache.log4j.patternlayout
Log4j.appender.a1.layout.conversionpattern=[%d{mm-dd HH:mm:ss}]%-5p%c{1} [%t]:%m%n

# file
Log4j.appender.file=org.apache.log4j.rollingfileappender
log4j.appender.file.file=${webapp.root}/web-inf/ Log/sshtest.log
log4j.appender.file.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.FILE.LAYOUT.CONVERSIONPATTERN=%D{HH:MM:SS}%-5p [%c] (%f:%l)-%m%n
log4j.appender.file.maxfilesize=4096kb

2. web.xml File Configuration Example
<web-app version= "3.0" xmlns= Http://java.sun.com/xml/ns/javaee "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ Web-app_3_0.xsd "> <display-name></display-name> <welcome-file-list> <welcome-file>inde x.jsp</welcome-file> </welcome-file-list> <filter> &LT;FILTER-NAME&GT;STRUTS2&LT;/FILTER-NAME&G
  	T <filter-class> Org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </ Filter-mapping> <!--pay attention to the order of each listener, otherwise there may be the above ${webapp.root}/web-inf/log/sshtest.log anomaly--> <!--log4j con Fig path--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/ web-inf/classes/log4j.properties</param-value&Gt </context-param> <!--webapp Root path--> <context-param> <param-name>webapprootkey</p Aram-name> <param-value>webapp.root</param-value> </context-param> <!--spring Config path --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/web-i
  Nf/classes/applicationcontext.xml</param-value> </context-param> <!--log4j Config listener-->
  <listener> <listener-class> Org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!--spring Contextloader listener--> <listener> <listener-class> org. Springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>


3. struts2.xml File Configuration Example
The following configuration allows you to automatically go to the Exception display page when an unexpected exception is applied; Note the order of each node
<struts> <package name= "its" extends= "Struts-default" > <interceptors> <interceptor-stack name= "Globalstack" > <interceptor-ref name= "defaultstack" > <param name= "exception.logenabled" >true</p aram> <param name= "Exception.loglevel" >ERROR</param> </interceptor-ref> </interceptor- stack> </interceptors> <default-interceptor-ref name= "Globalstack" ></default-interceptor-ref > <global-results> <result name= "error" >/error.jsp</result> </global-results> <glob Al-exception-mappings> <!--Here you can set up multiple exception types--> <exception-mapping result= "error" exception= " Java.lang.Exception "></exception-mapping> </global-exception-mappings> <action name=" Login " class= "Com.inspur.its.action.LoginAction" > <result name= "Success" >/Result.jsp</result> </action    
 > </package> </struts>

4. error.jsp page sample (only show the body part, import the struts2 tablib)
<body>
	<div align= "CENTER" >
		
An example of a truncated diagram following an exception appears:




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.