整体是使用aop +注解的方式实现
注解类
package cn.@@.project.handler.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogInfo {
String model();
String level();
String msg();
}
解析实现类
package cn.@@.project.handler.aspect;
import cn.hongt.project.handler.annotation.LogInfo;
import cn.hongt.project.handler.common.utils.RedisUtil;
import cn.hongt.project.handler.domain.entity.Loginfo;
import cn.hongt.project.handler.domain.entity.admin.SysUser;
import cn.hongt.project.handler.repository.LogInfoMapper;
import cn.hongt.util.common.utils.DateUtils;
import cn.hongt.util.common.utils.IdGenerator;
import cn.hongt.util.common.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
@Aspect
@Component
@Slf4j
public class LogInfoAspect {
@Autowired
private LogInfoMapper logInfoMapper;
@Autowired
private RedisUtil redisUtil;
@AfterReturning(value = "@annotation(cn.hongt.project.handler.annotation.LogInfo)", returning = "returnValue")
public void afterHandler(JoinPoint pjp, Object returnValue) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String username = "未知用户";
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader("token");
if (StringUtil.isNotBlank(token)) {
String UserString = (String) redisUtil.get(token);
JSONObject userJson = JSONObject.parseObject(UserString);
SysUser sysUser = JSON.toJavaObject(userJson, SysUser.class);
username = sysUser.getUsername();
}
}
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
method.setAccessible(true);
LogInfo logInfo = method.getAnnotation(LogInfo.class);
Object[] args = pjp.getArgs();
StringBuffer sbf = new StringBuffer(" ");
for (Object obs : args) {
try {
sbf.append(JSON.toJSONString(obs)).append(",");
}catch (Exception e){
continue;
}
}
String content = username +"在【"+ DateUtils.dateToString(new Date(), DateUtils.dateType5)+"】调用接口【" + method.getName() +"】传入参数为 【" + sbf.substring(0, sbf.toString().length() - 1) + "】。";
Loginfo loginfo = Loginfo.builder()
.id(IdGenerator.generateId())
.model(logInfo.model())
.level(logInfo.level())
.msg(logInfo.msg())
.username(username)
.resultMsg(content)
.optTime(new Date())
.build();
logInfoMapper.insert(loginfo);
}
}
代码注解使用样例
@ApiOperation(value = "用户管理-刪除用户", httpMethod = "POST")
@ApiOperationSupport(order = 6)
@PostMapping("/userremove")
@LogInfo(level = "1", model = "系统管理", msg = "删除用户")
public Result userremove(@Valid @RequestBody SysUserInput sysUserInput) {
boolean result = sysUserService.remove(sysUserInput);
return ResultUtil.success(result);
}
数据库
CREATE TABLE public.log_info (
id varchar NOT NULL,
username varchar NULL,
opt_time timestamp(0) NULL,
model varchar NULL,
msg varchar NULL,
resultmsg varchar NULL,
"level" varchar NULL,
CONSTRAINT log_info_pk PRIMARY KEY (id)
);
COMMENT ON TABLE public.log_info IS '日志记录';
COMMENT ON COLUMN public.log_info.id IS 'id';
COMMENT ON COLUMN public.log_info.username IS '操作人';
COMMENT ON COLUMN public.log_info.opt_time IS '操作时间';
COMMENT ON COLUMN public.log_info.model IS '归属模块';
COMMENT ON COLUMN public.log_info.msg IS '操作内容';
COMMENT ON COLUMN public.log_info.resultmsg IS '操作信息';
COMMENT ON COLUMN public.log_info."level" IS '日志等级';