输入,输出参数 日志监控!

一.注解类
package com.djhu.followup.control.advice;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**

  • @Author zw

  • @DATE 2019/6/5 9:22

  • @VERSION 1.0.0
    **/
    @Target({ElementType.METHOD,ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ApiLog {
    public String value();

    public boolean persistence() default false;

}
二.切面
package com.djhu.followup.control.advice;

import com.djhu.followup.entity.dto.ResultResp;
import com.djhu.followup.service.log.IPersistenceLogService;
import com.djhu.followup.service.log.RestLog;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**

  • @Author zw

  • @DATE 2019/6/5 9:21

  • @VERSION 1.0.0
    **/
    @Slf4j
    @Aspect
    @Component
    @EnableAspectJAutoProxy
    public class ApiLogAspect {

    @Autowired(required = false)
    private IPersistenceLogService persistenceLogService;

    private Gson gson = new Gson();

    @Pointcut("@annotation(com.djhu.followup.control.advice.ApiLog)")
    public void cutPoint(){
    }

    @Around(value=“cutPoint()&&@annotation(apiLog)”)
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint,ApiLog apiLog) throws Throwable {
    try {
    long start = System.currentTimeMillis();
    if (apiLog == null){
    return joinPoint.proceed();
    }
    Object[] args = joinPoint.getArgs();
    Object logArgs = getLogArgs(args);
    log.info("{} 接口,接到的请求为 {}",apiLog.value(),gson.toJson(logArgs));
    Object obj = joinPoint.proceed();
    long end = System.currentTimeMillis();
    //如果返回值必须是Result
    if (apiLog.persistence()){
    if (persistenceLogService != null){
    RestLog restLog = buildResultLog(joinPoint,obj);
    restLog.setStartTime(start);
    restLog.setStrStartTime(DateFormatUtils.format(start,RestLog.FORMAT_PATTERN));
    restLog.setEndTime(end);
    restLog.setStrEndTime(DateFormatUtils.format(end,RestLog.FORMAT_PATTERN));
    persistenceLogService.log(restLog);
    }
    }

         if(obj != null && (obj instanceof ResultResp)){
             ResultResp result = (ResultResp) obj;
             String strLog = String.format("%s 接口,返回界面的结果为 %s,用时为 %s",apiLog.value(),gson.toJson(result),String.valueOf(end-start));
             log.info(strLog);
             return obj;
         }else{
             log.warn("Return value:"+obj.getClass().getName()+" is not instance of "+ResultResp.class.getName()+" save api log may be error");
             String strLog = String.format("%s 接口,返回界面的结果为 %s,用时为 %s",apiLog.value(),gson.toJson(obj),String.valueOf(end-start));
             log.info(strLog);
             return obj;
         }
     }catch (Throwable t){
         log.error("接口 {} 出现异常!!!",apiLog.value(),t);
         throw t;
     }finally {
    
     }
    

    }

    private RestLog buildResultLog(ProceedingJoinPoint joinPoint, Object obj) {
    RestLog restLog= new RestLog();
    try {
    restLog.setProcessClass(joinPoint.getTarget().getClass().getName());
    restLog.setParameters(joinPoint.getArgs());
    restLog.setProcessMethod(joinPoint.getSignature().toString());
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    String realIP = request.getHeader(“X-Real-IP”);
    if(StringUtils.isEmpty(realIP)){
    restLog.setRemoteAddr(realIP);
    }else{
    restLog.setRemoteAddr(request.getRemoteAddr());
    }
    restLog.setLocalAddr(request.getLocalAddr());
    restLog.setLocalPort(request.getLocalPort());
    restLog.setUri(request.getRequestURI());
    }catch (Exception e){
    }
    return restLog;
    }

    private Object getLogArgs(Object[] args) {
    List list = new ArrayList<>();
    for (Object arg:args){
    if (arg instanceof ServletRequest){
    continue;
    }else {
    list.add(arg);
    }
    }
    if (CollectionUtils.isNotEmpty(list)){
    if (list.size() == 1){
    return list.get(0);
    }else {
    return list;
    }
    }else {
    return new ArrayList<>();
    }
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值