通过Aop跟自定义日志注解,实现系统日志的记录 持久化到数据库
1.自定义日志注解 实现
/**
* 定时日志注解
*
* @author lj
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
LogType type();;
}
2.aop 实现
/**
* 系统日志,切面处理类
*
* @author lj
*/
@Aspect
@Service
public class TaskLogAspect {
@Autowired
private TaskLogService taskLogService;
@Pointcut("@annotation(***annotation.SysLog)")//自定义注解位置
public void logPointCut() {
}
public TaskLogAspect(){
super();
System.out.println("已初始化sysLog日志切面类````````````````````````````````````````````````````````````");
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point){
long beginTime = System.currentTimeMillis();
Object result= null;
String error = "";//异常信息
Integer status = TaskConstant.ScheduleStatus.NORMAL.getValue(); //成功
try {
result = point.proceed();
}catch (Throwable e){
error = StringUtils.substring(e.toString(), 0, 2000);//异常信息
status = TaskConstant.ScheduleStatus.PAUSE.getValue();//失败
throw new RuntimeException(e);
}finally {
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveSysLog(point,time,error,status);
return result;
}
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time,String error,Integer status) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
TaskLogEntity taskLogEntity = new TaskLogEntity();
SysLog syslog = method.getAnnotation(SysLog.class);
if(syslog != null){
taskLogEntity.setOperation(syslog.value());
taskLogEntity.setType(syslog.type().getValue());
}
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
taskLogEntity.setMethodName(className + "." + methodName + "()");
//获取request
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//请求的参数
try{
// Map<String, Object> paramMap = getNameAndValue(joinPoint);
Map<String, Object> paramMap = showParams(request);
taskLogEntity.setParameter(JSONObject.toJSONString(paramMap));
//用户名
taskLogEntity.setUserName(UserUtils.getRealName());
taskLogEntity.setUserId(UserUtils.getUserId());
}catch (Exception e){
}
//设置IP地址
taskLogEntity.setIp(getRemortIP(request));
taskLogEntity.setResponseTime(time+"");
taskLogEntity.setError(error);
taskLogEntity.setStatus(status);
taskLogEntity.setCreateTime(DateUtils.getCurrentSecond());
//保存系统日志
taskLogService.save(taskLogEntity);
}
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
/**
* 获取代理方法的参数Map集合
* @param joinPoint
* @return
*/
Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {
Map<String, Object> param = new HashMap<>();
Object[] paramValues = joinPoint.getArgs();
String[] paramNames = ((CodeSignature)joinPoint.getSignature()).getParameterNames();
for (int i = 0; i < paramNames.length; i++) {
param.put(paramNames[i], paramValues[i]);
}
return param;
}
public static Map<String,Object> showParams(HttpServletRequest request) {
Map<String,Object> map = new HashMap<String,Object>();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length >0) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
map.put(paramName, paramValue);
}
}
}
return map;
}
}