编写日志统一处理类

一、使用元注解:
/**
* 系统日志注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

String value() default "";
1
}

二、编写系统日志,切面处理类:
package com.jianqiao.tslm.common.aspect;

import com.jianqiao.tslm.common.annotation.SysLog;
import com.jianqiao.tslm.common.utils.KeyUtil;
import com.jianqiao.tslm.entity.SysLogEntity;
import com.jianqiao.tslm.service.SysLogService;
import lombok.Data;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import com.jianqiao.tslm.common.utils.HttpContextUtils;
import com.jianqiao.tslm.common.utils.IPUtils;

/**
* 系统日志,切面处理类
*/
@Aspect
@Component
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;

@Pointcut("@annotation(com.jianqiao.tslm.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;

//保存日志
saveSysLog(point, time);

return result;
}

@AfterReturning("logPointCut()")
public void after(JoinPoint point) throws Throwable{
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
SysLog syslog = method.getAnnotation(SysLog.class);
if(syslog != null){

}
String className = point.getTarget().getClass().getName();
String methodName = signature.getName();
System.out.println(className + "." + methodName + "()" + syslog.value());
}

private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();

SysLogEntity sysLog = new SysLogEntity();
SysLog syslog = method.getAnnotation(SysLog.class);
if(syslog != null){
//注解上的描述
sysLog.setOperation(syslog.value());
}

//设置id
sysLog.setId(KeyUtil.genUniquekey());

//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLog.setMethod(className + "." + methodName + "()");

//请求的参数
Object[] args = joinPoint.getArgs();
try{
String params = new Gson().toJson(args[0]);
sysLog.setParams(params);
}catch (Exception e){
}

//获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//设置IP地址
sysLog.setIp(IPUtils.getIpAddr(request));

//用户名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
String username = “”;
sysLog.setUsername(username);

sysLog.setTime(time);
sysLog.setCreateDate(new Date());
//保存系统日志
sysLogService.insert(sysLog);
}
---------------------
作者:jay_888
来源:CSDN
原文:https://blog.csdn.net/jay_888/article/details/82012994
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/albert-think/p/10075227.html