SystemControllerLog.java 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;/** * 自定义注解
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;
/**
* 自定义注解 controller log
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemControllerLog {
String description() default "";
}
SystemLogAspect.java
import java.lang.reflect.Method;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.ronhan.pay.admin.annotion.SystemControllerLog;
import com.ronhan.pay.admin.security.Principal;
import com.ronhan.pay.admin.utils.SystemUtils;
import com.ronhan.pay.admin.utils.UserUtils;
import com.ronhan.pay.model.SysLog;
import com.ronhan.pay.services.system.SysLogService;
import net.sf.json.JSONObject;
/**
* 切点类
*/
@Aspect
@Component
public class SystemLogAspect {
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);
@Resource
private SysLogService sysLogService;
private HttpServletRequest request = null;
//Controller 切点
//@Pointcut("execution(* com.ronhan.pay.admin.controller..*.*(..))")
@Pointcut("@annotation(com.ronhan.pay.admin.annotion.SystemControllerLog)")
public void controllerAspect() {
}
@After("controllerAspect()")
public void doAfter(JoinPoint joinPoint) {
try {
logger.info("======================开始后置通知================");
//获取请求ip
request = getHttpServletRequest();
//获取ip
String ip = SystemUtils.getRemoteAddr(request);
//获取用户
Principal principal = UserUtils.getPrincipal();
//获取类名:
String className = joinPoint.getTarget().getClass().getName();
//获取方法名
String method = joinPoint.getSignature().getName();
//方法描述:
String desc = getControllerMethodDescription(joinPoint);
//获取请求参数
Object[] args = joinPoint.getArgs();
StringBuffer sb = new StringBuffer();
if(args.length > 0) {
for(int i = 0; i< args.length; i++) {
sb.append(args[i]).append(",");
}
if(sb.indexOf(",") > -1){
sb.deleteCharAt(sb.length() - 1);
}
}
SysLog sysLog = new SysLog();
sysLog.setLogMerno(principal.getUserId());
JSONObject json = new JSONObject();
json.put("level", "info");
JSONObject message = new JSONObject();
message.put("Class", className);
message.put("Method", method);
message.put("Description", desc);
message.put("Ip", ip);
message.put("Operater", principal.getDisplayName());
message.put("Args", sb.append("").toString());
json.put("message", message);
sysLog.setLogInfo(json.toString());
sysLog.setLogTime(new Date());
sysLog.setTransactionId(System.currentTimeMillis());
sysLogService.insertSelective(sysLog);
}catch(Exception e) {
logger.error("后置通知异常,异常信息:",e.getMessage());
}
}
@AfterThrowing(pointcut = "controllerAspect()",throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
try {
logger.info("======================开始异常通知================");
//获取请求ip
request = getHttpServletRequest();
//获取ip
String ip = SystemUtils.getRemoteAddr(request);
//获取用户
Principal principal = UserUtils.getPrincipal();
//获取类名:
String className = joinPoint.getTarget().getClass().getName();
//获取方法名
String method = joinPoint.getSignature().getName();
//方法描述:
String desc = getControllerMethodDescription(joinPoint);
//获取请求参数
Object[] args = joinPoint.getArgs();
StringBuffer sb = new StringBuffer();
if(args.length > 0) {
for(int i = 0; i< args.length; i++) {
sb.append(args[i]).append(",");
}
if(sb.indexOf(",") > -1){
sb.deleteCharAt(sb.length() - 1);
}
}
SysLog sysLog = new SysLog();
sysLog.setLogMerno(principal.getUserId());
JSONObject json = new JSONObject();
json.put("level", "error");
JSONObject message = new JSONObject();
message.put("Class", className);
message.put("Method", method);
message.put("Description", desc);
message.put("Throwing", desc);
message.put("Ip", ip);
message.put("Operater", principal.getDisplayName());
message.put("Args", sb.append("").toString());
JSONObject throwing = new JSONObject();
throwing.put("Error Message", e.getMessage());
json.put("Message", message);
json.put("Throwing", throwing);
sysLog.setLogInfo(json.toString());
sysLog.setLogTime(new Date());
sysLog.setTransactionId(System.currentTimeMillis());
sysLogService.insertSelective(sysLog);
}catch(Exception ex) {
logger.error("后置通知异常,异常信息:",ex);
}
}
/**
* 获取注解中对方法的描述信息 用于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;
}
/**
* 获取request
*/
public HttpServletRequest getHttpServletRequest() {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
return request;
}
}
