Search in sources :

Example 1 with LogEvent

use of vip.mate.core.log.event.LogEvent in project matecloud by matevip.

the class LogAspect method logAfterThrowing.

/**
 * 配置异常通知
 *
 * @param point join point for advice
 * @param e exception
 */
@AfterThrowing(pointcut = "pointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint point, Throwable e) {
    // 打印执行时间
    long startTime = System.nanoTime();
    CommonLog commonLog = new CommonLog();
    // 获取IP和地区
    String ip = RequestHolder.getHttpServletRequestIpAddress();
    String region = IPUtil.getCityInfo(ip);
    //  获取request
    HttpServletRequest request = RequestHolder.getHttpServletRequest();
    // 请求方法
    String method = request.getMethod();
    String url = request.getRequestURI();
    //  获取注解里的value值
    Method targetMethod = resolveMethod((ProceedingJoinPoint) point);
    Log logAnn = targetMethod.getAnnotation(Log.class);
    commonLog.setExecuteTime(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)).setIp(ip).setLocation(region).setMethod(method).setUrl(url).setTraceId(TraceUtil.getTraceId(request)).setType("2").setTitle(logAnn.value()).setException(ThrowableUtil.getStackTrace(e));
    // 设置MDC
    TraceUtil.mdcTraceId(TraceUtil.getTraceId(request));
    // 发布事件
    applicationContext.publishEvent(new LogEvent(commonLog));
    log.info("Error Result: {}", commonLog);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Log(vip.mate.core.log.annotation.Log) CommonLog(vip.mate.core.common.dto.CommonLog) LogEvent(vip.mate.core.log.event.LogEvent) CommonLog(vip.mate.core.common.dto.CommonLog) Method(java.lang.reflect.Method) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Example 2 with LogEvent

use of vip.mate.core.log.event.LogEvent in project matecloud by matevip.

the class LogAspect method recordLog.

/**
 * 配置环绕通知,使用在方法logPointcut()上注册的切入点
 * @param point
 * @return
 * @throws Throwable
 */
@Around("pointcut()")
public Object recordLog(ProceedingJoinPoint point) throws Throwable {
    Object result = new Object();
    //  获取request
    HttpServletRequest request = RequestHolder.getHttpServletRequest();
    // 判断为空则直接跳过执行
    if (ObjectUtils.isEmpty(request)) {
        return point.proceed();
    }
    //  获取注解里的value值
    Method targetMethod = resolveMethod(point);
    Log logAnn = targetMethod.getAnnotation(Log.class);
    // 打印执行时间
    long startTime = System.nanoTime();
    // 请求方法
    String method = request.getMethod();
    String url = request.getRequestURI();
    // 获取IP和地区
    String ip = RequestHolder.getHttpServletRequestIpAddress();
    String region = IPUtil.getCityInfo(ip);
    // 获取请求参数
    // Map<String, Object> paramMap = logIngArgs(point);
    // 参数
    Object[] args = point.getArgs();
    String requestParam = getArgs(args, request);
    // 计算耗时
    long tookTime = 0L;
    try {
        result = point.proceed();
    } finally {
        tookTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
    }
    //  如果是登录请求,则不获取用户信息
    String userName = null;
    if (!url.contains("oauth") && !(url.contains("code"))) {
        // 判断header是否存在,存在则获取用户名
        if (StringUtil.isNotBlank(SecurityUtil.getHeaderToken(request))) {
            userName = SecurityUtil.getUsername(request).getAccount();
        }
    }
    //  封装SysLog
    CommonLog commonLog = new CommonLog();
    commonLog.setIp(ip).setCreateBy(userName).setMethod(method).setUrl(url).setOperation(String.valueOf(result)).setLocation(StringUtils.isEmpty(region) ? "本地" : region).setTraceId(request.getHeader(MateConstant.MATE_TRACE_ID)).setExecuteTime(tookTime).setTitle(logAnn.value()).setParams(JSON.toJSONString(requestParam));
    log.info("Http Request: {}", JSONObject.toJSONString(commonLog));
    // 发布事件
    applicationContext.publishEvent(new LogEvent(commonLog));
    return result;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Log(vip.mate.core.log.annotation.Log) CommonLog(vip.mate.core.common.dto.CommonLog) LogEvent(vip.mate.core.log.event.LogEvent) JSONObject(com.alibaba.fastjson.JSONObject) Method(java.lang.reflect.Method) CommonLog(vip.mate.core.common.dto.CommonLog) Around(org.aspectj.lang.annotation.Around)

Aggregations

Method (java.lang.reflect.Method)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 CommonLog (vip.mate.core.common.dto.CommonLog)2 Log (vip.mate.core.log.annotation.Log)2 LogEvent (vip.mate.core.log.event.LogEvent)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AfterThrowing (org.aspectj.lang.annotation.AfterThrowing)1 Around (org.aspectj.lang.annotation.Around)1