一个基于spring aop实现的日志系统

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 com.tc.common.model.BusinessSystemOperationLog;
import com.tc.core.annotation.SystemControllerLog;
import com.tc.service.system.OperationLogService;

@Aspect
@Component
public class SystemLogAspect {

@Autowired
private OperationLogService operationLogService;
//本地异常日志记录对象
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);


//Service层切点
@Pointcut("@annotation(com.tc.core.annotation.SystemServiceLog)")
public void serviceAspect() {

}


//Controller层切点
@Pointcut("@annotation(com.tc.core.annotation.SystemControllerLog)")
public void controllerAspect() {

}

/**
* 前置通知 用于拦截Controller层记录用户的操作
*
* @param joinPoint 切点
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
//读取session中的用户
// User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
//请求的IP
String ip = request.getRemoteAddr();
try {
//*========控制台输出=========*//
logger.info("=====日志记录开始=====");
String content = getControllerMethodDescription(joinPoint);
logger.info("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
logger.info("方法描述:" + content);
// System.out.println("请求人:" + user.getName());
logger.info("请求IP:" + ip);
//*========数据库日志=========*//
BusinessSystemOperationLog log = new BusinessSystemOperationLog();
log.setContent(content);
log.setIpAddress(ip);
//保存数据库
operationLogService.insertSystemLog(log);
System.out.println("=====日志记录结束=====");
} catch (Exception e) {
e.printStackTrace();
//记录本地异常日志
logger.error("==日志记录异常==");
logger.error("异常信息:{}", e.getMessage());
}
}

/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param joinPoint 切点
* @return 方法描述
* @throws Exception
*/
public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(SystemControllerLog.class).description();
break;
}
}
}
return description;
}
}

xml 配置文件:<!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller-->
<aop:aspectj-autoproxy proxy-target-class="true" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值