首页>代码>springMVC+mybatis+oracle开发电信计费后台管理系统源码下载>/NETCTOSS/src/com/tarena/aspect/ExceptionLogger.java
package com.tarena.aspect;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.tarena.entity.Admin;

/**
 *	记录异常日志
 */
@Component
@Aspect
public class ExceptionLogger {
	
	@Resource
	private HttpServletRequest request;

	@Around("within(com.tarena.controller..*)")
	public Object log(ProceedingJoinPoint point) throws Exception {
		Object obj = null;
		try {
			obj = point.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
			// 记录错误日志
			Logger logger = Logger.getLogger(this.getClass());
			
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			if(admin != null) {
				String className = point.getTarget().getClass().getName();
				String method = point.getSignature().getName();
				String date = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss").format(new Date());
				
				StringBuffer sb = new StringBuffer();
				sb.append("用户[").append(admin.getName()).append("], ");
				sb.append("IP[").append(request.getRemoteHost()).append("], ");
				sb.append("在[").append(date).append("], 执行[");
				sb.append(className).append(".").append(method);
				sb.append("]时,发生如下异常:");
				logger.error(sb.toString());
			}
			
			StackTraceElement[] elems = e.getStackTrace();
			for(StackTraceElement elem : elems) {
				logger.error("\t" + elem.toString());
			}
			
			// 抛出异常
			throw new Exception(e);
		}
		return obj;
	}

}
最近下载更多
lyq6666666  LV5 2023年10月25日
王乐22222  LV10 2022年5月13日
wanglinddad  LV55 2021年12月27日
k915187267  LV2 2021年3月9日
user123456asdfsa  LV6 2021年3月5日
shahairui0315  LV3 2020年10月22日
阿德民  LV2 2020年7月22日
佳叶待你如初恋  LV6 2020年7月10日
lxw123  LV6 2020年6月21日
小小佳  LV10 2020年4月26日
最近浏览更多
chengguo123 4月2日
暂无贡献等级
yinfei305  LV2 2024年6月19日
123456wadff  LV3 2024年1月15日
WBelong  LV8 2023年12月26日
lyq6666666  LV5 2023年10月25日
阳光男孩阿葵 2023年7月14日
暂无贡献等级
zhuwenjing 2023年6月14日
暂无贡献等级
limuzi  LV7 2023年6月13日
chenfeng32  LV1 2023年4月19日
泓鼎168  LV20 2023年4月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友