All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.telewave.logger.aop.TwLoggerAop Maven / Gradle / Ivy

There is a newer version: 1.1.1.7
Show newest version
package com.telewave.logger.aop;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.telewave.logger.controller.OperationLogController;
import com.telewave.logger.entity.OperationLog;
import com.telewave.logger.entity.OperatorInfo;
import com.telewave.logger.service.OperationLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
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.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.UUID;

/**
 * Created by aiowang on 2018/7/19.
 */
@Aspect
@Component
public class TwLoggerAop {
    @Autowired
    OperationLogController operationLogController;

    @Autowired
    private Environment env;

    /*@Pointcut("execution(public * com.telewave.logger.controller.*.*(..))")*/
    @Pointcut("@annotation(com.telewave.logger.aop.TwLogger)")
    public void webLog(){
        System.out.println("Before-------------------");
    }

    @Before("webLog()&&"+"@annotation(twLogger)")
    public void deBefore(JoinPoint joinPoint,TwLogger twLogger) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String strOperatorInfo = request.getHeader("operator-info");
        OperatorInfo operatorInfo = null;
        OperationLog operationLog = new OperationLog();
        if (! StringUtils.isEmpty(strOperatorInfo)) {
            operatorInfo = new OperatorInfo();
            String result = URLDecoder.decode(strOperatorInfo, "UTF-8");
            if (!isJSONValid(result)) {
                /*LOGGER.fatal("表示OperatorInfo对象的json消息 ' " + result + " ' 不是合法的json字符串。");*/
            }else {
                operatorInfo = JSON.parseObject(result, OperatorInfo.class);
            }
        }else {

        }
        if(operatorInfo!=null){
            operationLog.setOperatorid(operatorInfo.getId()); //操作者ID
            operationLog.setOperator(operatorInfo.getLoginName()); //操作者登录名
            operationLog.setOperatordeptcode(operatorInfo.getOrganId());//操作者机构ID
            operationLog.setOperatordeptname(operatorInfo.getOrganName());//操作者机构名称
            operationLog.setSeatno(operatorInfo.getSeatNo());//操作者坐席
        }

        if(env.getProperty("app.name")!=null && env.getProperty("app.version")!=null){
            operationLog.setSoftware(env.getProperty("app.name")); //软件名称
            operationLog.setVersion(env.getProperty("app.version")); //软件版本号
        }
        operationLog.setCreatelogdate(new Date()); //日志时间
        operationLog.setId(UUID.randomUUID().toString().replaceAll("-","")); //唯一ID

        operationLog.setModel(twLogger.modelName());
        operationLog.setOperation(twLogger.mothodType()[0].getIndex());
        operationLog.setClientip(request.getRemoteAddr());
        operationLog.setCreatedate(new Date());
        operationLogController.insert(operationLog);

        // 记录下请求内容
        /*System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));*/

    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        System.out.println("方法的返回值 : " + ret);
    }

    //后置异常通知
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp){
        System.out.println("方法异常时执行.....");
    }

    //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println("方法最后执行.....");
    }

    //环绕通知,环绕增强,相当于MethodInterceptor
    @Around("webLog()")
    public Object arround(ProceedingJoinPoint pjp) {
        System.out.println("日志记录-方法环绕start.....");
        if(env.getProperty("app.name")==null || env.getProperty("app.version")==null){
            System.out.println("未配置程序名和版本号,不能进行日志上传,请在配置文件中配置");
            return null;
        }
        try {
           /* // 接收到请求,记录请求内容
            Object target = pjp.getTarget();
            //拦截的方法名称
            String methodName = pjp.getSignature().getName();
            //拦截的放参数类型
            Class[] parameterTypes = ((MethodSignature)pjp.getSignature()).getMethod().getParameterTypes();
            Method method = target.getClass().getMethod(methodName, parameterTypes);
            Annotation  annotation = method.getAnnotation(TwLogger.class);
            if(annotation==null){
                return null;
            }*/
            Object o =  pjp.proceed();
            System.out.println("日志记录-方法环绕proceed,结果是 :" + o);
            return o;

        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }

    }


    public final boolean isJSONValid(String test) {
        try {
            final ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(test);
            return true;
        }
        catch (IOException e) {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy