package com.newfiber.common.aspect; import com.alibaba.fastjson.JSONObject; import com.newfiber.common.utils.HttpContextUtils; import com.newfiber.common.utils.IPUtils; import com.newfiber.modules.sys.entity.SysLog; import com.newfiber.modules.sys.service.impl.SyslogService; import java.lang.reflect.Method; import java.util.Date; 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.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 系统日志,切面处理类 * @author hy * @date 2017年7月3日 上午11:07:35 */ @Aspect @Component public class SysLogAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private SyslogService syslogService; @Pointcut("@annotation(com.newfiber.common.annotation.SysLog)") public void logPointCut() { } @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); //执行方法 Object result = point.proceed(); //执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; //保存日志 try { //获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); String ip = IPUtils.getIpAddr(request); String userNo = request.getHeader("userNo"); String url = request.getServletPath(); saveSysLog(point, time, result.toString(),userNo,url,ip); } catch(Exception e) { System.out.println("exec func around Exception ,error="+e); } return result; } public void saveSysLog(ProceedingJoinPoint joinPoint,long time,String strResponeResult,String sid,String url,String ip){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); com.newfiber.common.annotation.SysLog syslogAccnotation = method.getAnnotation(com.newfiber.common.annotation.SysLog.class); if(syslogAccnotation != null){ //注解上的描述 sysLog.setOperation(syslogAccnotation.value()); sysLog.setActionType(syslogAccnotation.actionType()); } //请求的方法名 //String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); //sysLog.setMethod(className + "." + methodName + "()"); sysLog.setMethod(methodName); //请求的参数 Object[] args = joinPoint.getArgs(); if(args.length>0) { //String params = new Gson().toJson(args[0]); String params = JSONObject.toJSONString(args[0]);; sysLog.setParams(params); } //设置IP地址 sysLog.setIp(ip); sysLog.setSid(sid); //String url = request.getServletPath(); sysLog.setUrl(url); //平台业务编号 sysLog.setPlatformName("huludao-scada"); //执行结果 //String result = new Gson().toJson(resultObj); String result = strResponeResult; sysLog.setResult("==============="+result); //执行时长(毫秒) sysLog.setTime(time); sysLog.setCreateDate(new Date()); //保存系统日志 syslogService.insert(sysLog); } }