aop java操作日志

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class LogAspect {

    @Around("@annotation(com.公司名.ActionDescriptionAttribute)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = null;
        long time = System.currentTimeMillis();
        try {
            res =  joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            return res;
        } finally {
            try {
                //方法执行完成后增加日志
                addOperationLog(joinPoint,res,time);
            }catch (Exception e){
                System.out.println("LogAspect 操作失败:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    AuditLogService auditLogService;

    private void addOperationLog(JoinPoint joinPoint, Object res, long time){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        OperationLog operationLog = new OperationLog();
        Object[] args = joinPoint.getArgs();
        String token = httpServletRequest.getHeader("Authorization");
        String appid = httpServletRequest.getHeader("appid");

       // ApplicationCertificateInputDTO inputString = (ApplicationCertificateInputDTO) args[2];
        operationLog.accessId = appid;
        operationLog.accessToken = token ;
        operationLog.appSource = HttpUtil.getIpAddr(httpServletRequest);
        //operationLog.setCertificateNumber(infoString.toString());
       // operationLog.data = JSON.toJSONString(inputString.getInfo());
        operationLog.appOprDatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" ).format(new Date());
        operationLog.result = JSON.toJSONString(res);
        operationLog.timeOfUse = time;
        ActionDescriptionAttribute actionDescriptionAttribute =signature.getMethod().getAnnotation(ActionDescriptionAttribute.class);
        if (actionDescriptionAttribute != null) {
            operationLog.appInterfaceName = actionDescriptionAttribute.name();
            String name = signature.getMethod().getName();
            operationLog=saveLogByMethodName(joinPoint,operationLog,name);
        }
        //TODO 这里保存日志
        System.out.println("记录日志:" + operationLog.toString());
//        operationLogService.insert(operationLog);
        auditLogService.auditLog(operationLog);
        //System.out.println(signature.getMethod().getName());
    }

    private OperationLog saveLogByMethodName(JoinPoint joinPoint,OperationLog operationLog,String methodName){
        Object[] args = joinPoint.getArgs();
        switch (methodName){
            case "getApplicatCertificate":
                InfoString infoString=(InfoString)args[2];
                operationLog.data=JSON.toJSONString(infoString.getInfo());
                break;
            case "ApplicatCertificate":
                ApplicationCertificateInputDTO inputString = (ApplicationCertificateInputDTO) args[2];
                operationLog.data = JSON.toJSONString(inputString.getInfo());
                break;
            case "GetCertificateList":
                ShowInputDTO info=(ShowInputDTO)args[1];
                operationLog.data = JSON.toJSONString(info.getInfo());
                break;
            case "getDctCertificateInfo":
                ShowCertificationInputDTO param=(ShowCertificationInputDTO)args[1];
                operationLog.data = JSON.toJSONString(param.getCertNum());
                break;
            case "getHosptCertificateInfo":
                ShowCertificationInputDTO param1=(ShowCertificationInputDTO)args[1];
                operationLog.data = JSON.toJSONString(param1.getCertNum());
                break;
            case "getNurseCertificateInfo":
                ShowCertificationInputDTO param2=(ShowCertificationInputDTO)args[1];
                operationLog.data = JSON.toJSONString(param2.getCertNum());
                break;
            case "getQrCode":
                GetqrCodeInputDTO getqrCodeInputDTO=(GetqrCodeInputDTO)args[1];
                operationLog.data = JSON.toJSONString(getqrCodeInputDTO);
                break;
            case "getInfoByqr":
                InfoByqrInputDTO inputDTO=(InfoByqrInputDTO)args[0];
                operationLog.data=JSON.toJSONString(inputDTO);
                break;
            case "whetherCouldApplication":
                JudgCertificateInputDTO judgCertificateInputDTO=(JudgCertificateInputDTO)args[1];
                operationLog.data=JSON.toJSONString(judgCertificateInputDTO);
                break;
        }
        return operationLog;
    }
}

import java.lang.annotation.*;

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionDescriptionAttribute {
    String name();
}

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

public class HttpUtil {




    /**
     * @Description: 获取客户端IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        // 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        if(ip != null && ip.length() > 15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值