Skip to content

Instantly share code, notes, and snippets.

@ttfcfc
Forked from yangl/LogAspect.java
Created January 12, 2017 03:48

Revisions

  1. @yangl yangl created this gist Aug 16, 2013.
    74 changes: 74 additions & 0 deletions LogAspect.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    package com.jd.couponservice.service.aspect;

    import java.util.HashMap;
    import java.util.Map;

    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.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.util.StopWatch;

    import com.jd.couponservice.common.mapper.JsonMapper;

    /**
    * 超时打印日志
    *
    * @author YANGLiiN
    * @date 13-6-4 上午9:28
    */

    @Aspect
    public class LogAspect {

    Logger logger = LoggerFactory.getLogger(LogAspect.class);

    private boolean enable = true;
    private Map<String, Long> methodTime = new HashMap<String, Long>();

    @Pointcut("execution(public * com.jd.couponservice.client.service.*.*(..)) || execution(public * com.jd.couponservice.cxfservice.*.*(..))")
    public void logPointcut() {}

    @Around("logPointcut()")
    public Object execDegrade(ProceedingJoinPoint jp) throws Throwable {
    MethodSignature ms = (MethodSignature) jp.getSignature();
    String methodName = ms.getName();

    StopWatch sw = new StopWatch();
    sw.start();

    try {
    return jp.proceed();
    }
    finally {
    sw.stop();
    long time = sw.getTotalTimeMillis();

    if (enable && methodTime.keySet().contains(methodName)) {
    long confTime = methodTime.get(methodName);

    if (time > confTime) {
    logger.error("****api method[{}] exec time[{}ms],args[{}]****",
    methodName,
    time,
    JsonMapper.nonEmptyMapper().toJson(jp.getArgs()));

    }

    }

    }

    }

    public void setEnable(boolean enable) {
    this.enable = enable;
    }

    public void setMethodTime(Map<String, Long> methodTime) {
    this.methodTime = methodTime;
    }
    }
    69 changes: 69 additions & 0 deletions spring-config-log.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    default-autowire="byName">


    <bean id="logAspect" class="com.jd.couponservice.service.aspect.LogAspect">
    <property name="enable" value="true"/>
    <property name="methodTime">
    <map>
    <entry key="useCoupon">
    <value>100</value>
    </entry>
    <entry key="rollbackCoupon">
    <value>100</value>
    </entry>
    <entry key="updateCouponEndTime">
    <value>100</value>
    </entry>
    <entry key="usedAfterRollbackCoupon">
    <value>100</value>
    </entry>

    <entry key="getCouponById">
    <value>100</value>
    </entry>
    <entry key="getCouponsByIds">
    <value>100</value>
    </entry>
    <entry key="getCouponsByOrderIdCount">
    <value>100</value>
    </entry>
    <entry key="getCouponsByOrderId">
    <value>100</value>
    </entry>
    <entry key="getCouponsByOrderIds">
    <value>100</value>
    </entry>
    <entry key="getCouponsByPin">
    <value>100</value>
    </entry>
    <entry key="getCouponByKey">
    <value>100</value>
    </entry>
    <entry key="lockedCouponCount">
    <value>100</value>
    </entry>

    <entry key="getAvailableCoupons">
    <value>100</value>
    </entry>
    <entry key="getAllCouponsWithCheck">
    <value>100</value>
    </entry>
    <entry key="isCategoryLimit">
    <value>100</value>
    </entry>
    <entry key="isVenderLimitForAnyOne">
    <value>100</value>
    </entry>
    </map>
    </property>
    </bean>

    </beans>