aop记录日志

这里用注解的形式来完成日志记录

先贴代码

@Aspect
@Component
public class LogAspect{
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

@Pointcut("@annotation(com.crsri.common.annotation.Log)")
public void logPointCut() {
}
@Autowired
private LogService logService;

@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
    //long beginTime = System.currentTimeMillis();
    // 执行方法
    Object result = point.proceed();
    // 执行时长(毫秒)
    //long time = System.currentTimeMillis() - beginTime;
    //异步保存日志
    saveLog(point);
    return result;
}

aop记录日志

这里定义一个方法呼应前面的方法将point传过来
void saveLog(ProceedingJoinPoint joinPoint) throws InterruptedException {
//创建对象
LogDO sysLog = new LogDO();
//获取被代理对象
BasicController basicController=(BasicController)joinPoint.getTarget();
//get方法获取operationinfo
String operationinfo = basicController.getOperationinfo();
sysLog.setOperationInfo(operationinfo);
//get方法获取operationtype
String operationtype = basicController.getOperationtype();
sysLog.setOperationType(operationtype);
//get方法获取projectID
String projectID = basicController.getProjectID();
sysLog.setProjectId(projectID);
// 将BasicController重置
basicController.setLogInfo(null, null, null);
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//Method method = signature.getMethod();
/* Log syslog = method.getAnnotation(Log.class); /
/

* if (syslog != null) { // 注解上的描述 sysLog.setOperationInfo(operationinfo); }
*/
// 请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLog.setOperationFunction((className + “.” + methodName + “()”));
// 请求的参数
Object[] args = joinPoint.getArgs();
try {
String params = JSONUtils.beanToJson(args[0]).substring(0, 4999);
sysLog.setParams(params);
} catch (Exception e) {

    }
	/*
	 * String[] parameterNames = signature.getParameterNames(); for (int i = 0; i <
	 * parameterNames.length; i++) { String string = parameterNames[i];
	 * if(parameterNames[i].equals("pro")) {
	 * System.out.println("刘飞------------------"+args[i]); } }
	 */
    // 获取request
    HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
    // 设置IP地址
    sysLog.setLogIP(IPUtils.getIpAddr(request));
    sysLog.setOperationAPI(HttpContextUtils.getApiUrL(request));
    // 用户名
    User currUser = ShiroUtil.getUser();
    if (null == currUser) {
        if (null != sysLog.getParams()) {
            sysLog.setUserID("");
            sysLog.setParams(sysLog.getParams());
        } 
    } else {
        sysLog.setUserID(ShiroUtil.getUserId());
        sysLog.setUsername(ShiroUtil.getUser().getUsername());
    }
    // 系统当前时间
    Date date = new Date();
    sysLog.setOperationTime(date);
    sysLog.setOperationId(UUID.randomUUID().toString());
    logService.addLog(sysLog);//添加日志
}

aop记录日志

// 请求的参数
Object[] args = joinPoint.getArgs();
try {
String params = JSONUtils.beanToJson(args[0]).substring(0, 4999);
sysLog.setParams(params);
} catch (Exception e) {

    }

/*
* String[] parameterNames = signature.getParameterNames(); for (int i = 0; i <
* parameterNames.length; i++) { String string = parameterNames[i];
* if(parameterNames[i].equals(“pro”)) {
* System.out.println(“刘飞------------------”+args[i]); } }
*/
这里是将日志中参数取出来进行一个遍历
parameterNames是参数名字
args[i]是参数的值

// 获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
// 设置IP地址
sysLog.setLogIP(IPUtils.getIpAddr(request));
sysLog.setOperationAPI(HttpContextUtils.getApiUrL(request));
// 用户名
User currUser = ShiroUtil.getUser();
if (null == currUser) {
if (null != sysLog.getParams()) {
sysLog.setUserID("");
sysLog.setParams(sysLog.getParams());
}
} else {
sysLog.setUserID(ShiroUtil.getUserId());
sysLog.setUsername(ShiroUtil.getUser().getUsername());
}
// 系统当前时间
Date date = new Date();
sysLog.setOperationTime(date);
sysLog.setOperationId(UUID.randomUUID().toString());
logService.addLog(sysLog);//添加日志
aop记录日志

public class HttpContextUtils {
public static HttpServletRequest getHttpServletRequest() {
return((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
}

public static String getApiUrL(HttpServletRequest req) {
	return req.getRequestURI();
}

}

上一篇:【5分钟课堂-Java】1.0.0-Java发展历程


下一篇:java语言的发展简史