private static final long serialVersionUID = 1000002;
protected String errcode;
protected String errmsg;
protected String errtype;
protected Map
protected static String errpre = “”;
protected Exception exception;
protected Logger logger;
public String getErrcode() {<br /> return errcode;<br /> }
public void setErrcode(String errcode) {<br /> this.errcode = errcode;<br /> }
public String getErrmsg() {<br /> return errmsg;<br /> }
public void setErrmsg(String errmsg) {<br /> this.errmsg = errmsg;<br /> }
public String getMessage() {<br /> return errmsg;<br /> }
public String getErrtype() {<br /> return errtype;<br /> }
public void setErrtype(String errtype) {<br /> this.errtype = errtype;<br /> }
public Map<String, Object> getErrdata() {<br /> return errdata;<br /> }
public void setErrdata(Map<String, Object> errdata) {<br /> this.errdata = errdata;<br /> }
public BaseException(String errcode, String errmsg) {<br /> this(errcode, errmsg,errpre);<br /> }
public BaseException(String errcode, String errmsg,String errpre) {<br /> this(errcode, errmsg,errpre, BaseErrCst.ERRTYPE_ERR_TIPS, null, null, null);<br /> }
/**<br /> * <br /> * @param errcode 错误代码<br /> * @param errmsg 错误信息<br /> * @param errtype 错误类型<br /> * @param errdata 错误数据<br /> */<br /> public BaseException(String errcode, String errmsg,String errpre, String errtype, Map<String, Object> errdata, Logger logger, Exception e) {<br /> this.errcode = errpre + errcode;<br /> this.errmsg = errmsg;<br /> this.errtype = errtype;<br /> this.errdata = errdata;<br /> this.exception = e;<br /> this.logger = logger;<br /> }
/**<br /> * 打印错误日志及抛异常<br /> * @param errcode<br /> * @param errmsg<br /> * @param logger<br /> * @param e<br /> */<br /> public BaseException(String errcode, String errmsg,String errpre, Logger logger, Exception e) {<br /> this(errcode, errmsg,errpre, BaseErrCst.ERRTYPE_WARN, null, logger, e);<br /> printError();<br /> }
public void printError() {<br /> if (logger != null && this.exception != null) {<br /> if (!(this.exception instanceof BaseException)) {<br /> ErrorLogger.error(logger, errcode, errmsg, exception);<br /> } else {<br /> ErrorLogger.error(logger, errcode, errmsg);<br /> }<br /> }<br /> }
package cn.jsfund.param.common.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
}
package cn.jsfund.param.web;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.jsfund.param.common.utils.UserInfoByToken;
import cn.jsfund.param.model.UserInfo;
@Aspect
@Component
public class LogAspect {
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
@Autowired<br /> private UserInfoByToken userInfoByToken;
@Pointcut("@annotation(cn.jsfund.param.common.annotation.Log)")<br /> public void logPointcut() {<br /> }
@Around("logPointcut()")<br /> public Object logHandler(ProceedingJoinPoint process) throws Throwable {<br /> long startTime = System.currentTimeMillis();<br /> HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();<br />// HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
MethodSignature methodSignature = (MethodSignature) process.getSignature();
Method method = methodSignature.getMethod();
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
Object[] args = process.getArgs();
StringBuilder params = new StringBuilder();
// UserInfo userInfo = (UserInfo) HttpUtil.getAttribute(Constant.LOGIN_USERINFO);
UserInfo userInfo = userInfoByToken.getUserInfoByToken(request);
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof org.apache.catalina.connector.RequestFacade || arg instanceof org.apache.catalina.connector.ResponseFacade) {
continue;
}
params.append(arg == null ? "" : arg.toString());<br /> params.append(";");<br /> }<br /> log.info("请求request:请求时间:{},请求用户:{},请求类名:{},请求方法:{},请求参数:{}", startTime, userInfo == null ? "" : userInfo.getUserid(), className, methodName,<br /> params.toString());
JsonResult result = (JsonResult) process.proceed();
JsonResult resultclone = new JsonResult();// process.proceed();
BeanUtils.copyProperties(result, resultclone);
resultclone.setData(“忽略”);
long costTime = System.currentTimeMillis() - startTime;
log.info(“请求response:请求时间:{},请求用户:{},请求耗时:{},请求类名:{},请求方法:{},请求结果:{}”, startTime, userInfo == null ? “” : userInfo.getUserid(), costTime, className,
methodName, resultclone);
return result;
}
}
package cn.jsfund.base.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import com.alibaba.dubbo.common.json.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.jsfund.base.common.exception.BaseErrCodeCst;
import cn.jsfund.base.common.exception.BaseErrCst;
import cn.jsfund.base.common.exception.BaseException;
public class BaseController {
private static Logger logger = LoggerFactory.getLogger(BaseController.class);
@InitBinder<br /> protected void initBinder(WebDataBinder binder) {<br /> SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");<br /> dateFormat.setLenient(false);<br /> binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));<br /> }
@ExceptionHandler(Exception.class)<br /> public void handleException(Exception ex, HttpServletRequest request, HttpServletResponse response) {<br /> if (ex instanceof BaseException) {<br /> logger.warn("发生WEB异常" + ex.getMessage());<br /> } else {<br /> logger.warn("发生未捕获的WEB异常", ex);<br /> }
// 判断请求的类型,如果是ajax,则返回标准的错误ajax异常,否则重定向到错误处理页面
boolean isajaxreq = “XMLHttpRequest”.equals(request.getHeader(“X-Requested-With”));
try {
if (isajaxreq) {
JsonResult result = new JsonResult();
String sMessage = ex.getMessage();
if (ex instanceof BaseException) {
BaseException be = ((BaseException) ex);
String errcode = be.getErrcode();
String errtype = be.getErrtype();
Map
/*适配之前异常处理start*/
// 异常分类
// 警告提示
if (errcode.startsWith(BaseErrCst.EXCEPTION_PROMPT) || BaseErrCst.ERRTYPE_WARN.equals(errtype)) {
result.setResult(2);
// 需人工点击确认的提示
} else if (errcode.startsWith(BaseErrCst.EXCEPTION_CONFIRM) || BaseErrCst.ERRTYPE_ERR_SURE.equals(errtype)) {
result.setResult(3);
// 需人工选择确认或取消
} else if (errcode.startsWith(BaseErrCst.EXCEPTION_CONFCAN) || BaseErrCst.ERRTYPE_ERR_SANDC.equals(errtype)) {
result.setResult(4);
} else if (errcode.startsWith(BaseErrCst.EXCEPTION_MESSAGE)) {
// 特殊 异常中 格式化JSON数据 解析后返回
if (sMessage.contains(“|||”)) {
String data = sMessage.substring(sMessage.indexOf(“|||”) + 3, sMessage.lastIndexOf(“|||”));
if (StringUtils.isNotBlank(data)) {
@SuppressWarnings(“unchecked”)
Map
result.setData(resultdata);
}
}
result.setResult(6);<br /> // 异常提示<br /> } else {<br /> result.setResult(1);<br /> }<br /> // 如果提示信息需要从后台获取数据,则将返回类型设置为6<br /> if (errdata != null) {<br /> result.setData(errdata);<br /> result.setResult(6);<br /> }<br /> /*****************适配之前异常处理*end************************/
if (("B"+(BaseErrCodeCst.NONLOGIN_ERR)).equals(errcode)) {<br /> result.setResult(1);<br /> result.setCode(BaseErrCodeCst.NONLOGIN_ERR);<br /> }<br /> result.setMessage(ex.getMessage());<br /> } else {<br /> result.setResult(1);<br /> result.setMessage("系统错误,请与管理员联系。");<br /> }
ObjectMapper mapper = new ObjectMapper();<br /> String s = "";<br /> byte[] content = s.getBytes();<br /> try {<br /> s = mapper.writeValueAsString(result);<br /> content = s.getBytes("UTF-8");<br /> } catch (Exception e) {<br /> logger.warn("转换json对象错误", e);<br /> logger.warn("系统异常" + e + e.getMessage());<br /> }<br /> response.setCharacterEncoding("UTF-8");<br /> response.setContentType("application/json;charset=UTF-8");<br /> response.getOutputStream().write(content);
} else {<br /> // 传送错误提示信息<br /> if (ex instanceof BaseException) { // busiException 显示错误代码和错误描述<br /> request.setAttribute("errormessage", ex.getMessage());<br /> } else {// 非busiException ,统一提示信息<br /> request.setAttribute("errormessage", "系统错误,请联系系统管理员");<br /> // response.sendRedirect("/error?errormessage="+ex.getMessage());<br /> }<br /> request.setAttribute("errURLNeed", "N");<br /> request.setAttribute("errobj", ex);<br /> request.getRequestDispatcher("/error").forward(request, response);<br /> }
} catch (Exception e2) {
logger.warn(“系统错误”, e2);
}
}
}
package cn.jsfund.base.common.exception;
import org.slf4j.Logger;
public class ErrorLogger {
private String errcode;
private String errmsg;
public String getErrcode() {
return errcode;
}
public void setErrcode(String errcode) {<br /> this.errcode = errcode;<br /> }
public String getErrmsg() {<br /> return errmsg;<br /> }
public void setErrmsg(String errmsg) {<br /> this.errmsg = errmsg;<br /> }
public static void error(Logger logger, String errcode, String errmsg) {<br /> logger.error("errcode={},errmsg={}", errcode, errmsg);<br /> }
public static void error(Logger logger, String errcode, String errmsg,Exception e) {<br /> logger.error("errcode={},errmsg={}", errcode, errmsg,e);<br /> <br /> <br /> }<br />}