使用Springboot Aop 注解的方式 实现
1.声明日志注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
String description() default "";
}
2.用于日志记录的 DO
public class SystemLogDO {
/** id*/
private String id;
/** 操作用户id*/
private Long userId;
/** 类名*/
private String clazz;
/** 方法名*/
private String method;
/** 描述*/
private String description;
/** 操作时间*/
private Date operateTime;
/** 操作ip*/
private String ip;
/** 请求参数*/
private String args;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz == null ? null : clazz.trim();
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method == null ? null : method.trim();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public Date getOperateTime() {
return operateTime;
}
public void setOperateTime(Date operateTime) {
this.operateTime = operateTime;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip == null ? null : ip.trim();
}
public String getArgs() {
return args;
}
public void setArgs(String args) {
this.args = args == null ? null : args.trim();
}
}
3.记录日志
@Aspect
@Component
@SuppressWarnings("all")
public class SystemLogAspect {
private static final Logger logger = LoggerFactory.getLogger(ResourceFilterAspect.class);
@Autowired
private SystemLogService systemLogService;//日志服务方法
@Autowired
private ObjectMapper objectMapper;//序列化成JSON的工具
private Class[] ignoreClasses = {HttpServletRequest.class, HttpServletResponse.class};//需要忽略的类
private String[] ignoreParams = {};//需要忽略的参数
@Pointcut("@annotation(com.jandar.portal.annotation.SystemLog)")
public void logsAspect() {//注解切点
System.out.println("log");
}
@Around("logsAspect()")
public Object logRecording(ProceedingJoinPoint pjp) throws Throwable {
try {
//获取方法签名
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
SystemLog log = method.getAnnotation(SystemLog.class);
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = IPUtil.getRealIP(request);//获取ip
Map<String, Object> argMap = toArgsMap(pjp.getArgs(), signature, ignoreClasses, ignoreParams);//获取参数
Date operateTime = new Date();
String description = log.description();//获取注解中的描述
String clazzName = pjp.getTarget().getClass().getTypeName();
String methodName = pjp.getSignature().getName();
//记录日志
SystemLogDO systemLogDO = new SystemLogDO();
systemLogDO.setIp(ip);
systemLogDO.setDescription(description);
systemLogDO.setClazz(clazzName);
systemLogDO.setMethod(methodName);
systemLogDO.setOperateTime(operateTime);
systemLogDO.setUserId(CurrentUserUtil.MANAGER_USER_ID());
try {
systemLogDO.setArgs(objectMapper.writeValueAsString(argMap));
} catch (Exception e) {
logger.error("Arg属性异常");
}
systemLogService.add(systemLogDO);//保存日志
} catch (Exception e) {
logger.error(e.getMessage());
}
return pjp.proceed();
}
/**
* 提取有效参数
*
* @param args
* @param methodSignature
* @param ignoreClasses
* @param ignoreParams
* @return
*/
protected Map<String, Object> toArgsMap(Object[] args, MethodSignature methodSignature,
Class<?>[] ignoreClasses, String[] ignoreParams) {
Map<String, Object> paramMap = Maps.newHashMap();
for (int i = 0; i < methodSignature.getParameterNames().length; i++) {
String paramName = methodSignature.getParameterNames()[i];
if (!isIgnoreParamType(args[i], ignoreClasses)
&& !isIgnoreParam(paramName, ignoreParams)) {
paramMap.put(paramName, args[i]);
}
}
return paramMap;
}
/**
* 忽略参数类型
*
* @param param
* @param classes
* @return
*/
protected boolean isIgnoreParamType(Object param, Class<?>[] classes) {
for (Class<?> aClass : classes) {
if (aClass.isInstance(param)) {
return true;
}
}
return false;
}
/**
* 忽略参数
*
* @param name
* @param paramNames
* @return
*/
protected boolean isIgnoreParam(String name, String[] paramNames) {
for (String paramName : paramNames) {
if (name.equals(paramName)) {
return true;
}
}
return false;
}
网友评论