首页>代码>Spring MVC+mybatis+mysql开发电信计费后台管理系统源码下载>/netctoss_demo/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;
	}

}
最近下载更多
微信网友_6602457430806528  LV6 2023年9月21日
xingbing  LV9 2023年2月15日
微信网友_6248713511227392  LV11 2022年12月5日
ming_123_9715  LV23 2022年9月22日
王乐22222  LV10 2022年5月13日
贝拉今天打什么  LV2 2022年3月28日
微笑刺客  LV15 2021年11月25日
葡萄树下酒庄  LV9 2021年6月18日
nitama  LV19 2021年5月17日
whsbuudjxbw  LV1 2021年4月6日
最近浏览更多
admin_z  LV22 1月29日
123456wadff  LV3 1月15日
black8angel  LV4 2023年12月22日
shuangfu  LV25 2023年12月2日
微信网友_6602457430806528  LV6 2023年9月21日
矿泉水  LV30 2023年8月10日
zhuwenjing 2023年6月14日
暂无贡献等级
c469893928  LV2 2023年6月10日
lpclpclpc  LV4 2023年5月30日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友