1编写注解
项目包下增加一个annotation的package,增加一个自定义的注解,如MyLog,里面可以写上自定义的属性
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 日志注解
* @author xxx
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyLog {
/**
* 自定义属性
* @return
*/
String type() default "xxx";
}
2编写aop处理
增加一个compnonent的package,增加一个MyLogAspect,此处使用的是环绕切,可根据业务调
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
/**
* Description:
* 日志拦截器
*
* @author xxx 2018/07/23.
*/
@Aspect
@Component
public class MyLogAspect {
private final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
@Autowired
private XXLogService logService;
@Pointcut(value = "@annotation(com.xxx.annotation.MyLog)")
private void pointCut() {
}
@Around(value = "pointCut() && @annotation(logType)")
public Object around(ProceedingJoinPoint joinPoint, MyLog logType) {
Timestamp start = new Timestamp(System.currentTimeMillis());
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Date createTime = new Date();
String ip = WebUtil.getIpAddr(request);
String url = request.getRequestURL().toString();
String sessionId = request.getRequestedSessionId();
Object outObj = null;
//入参对应的下标args[index]
String paramData = JSON.toJSONString(joinPoint.getArgs()[0],
SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
//执行完后返回方法的出参
outObj = joinPoint.proceed();
Timestamp end = new Timestamp(System.currentTimeMillis());
//方法执行的costTime
long cost = end.getTime() - start.getTime();
//可以做其他处理***
return outObj;
}
}
3添加注解
给需要处理的方法上加上自定义的注解,如controller,当然可以加在service中,或带有@Component注解的类中的方法上
import com.alibaba.fastjson.JSON;
import com.xxx.annotation.MyLog;
import com.xxx.dto.RespResult;
import com.xxx.dto.req.ILogReqParams;
import com.xxx.service.ILogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: xxx
* @Date: Created in 下午6:01 2018/6/6
* @Des:
* @Modifyed By:
*/
@RestController
@RequestMapping("/my/index")
public class ILogController {
private Logger LOGGER = LoggerFactory.getLogger(ILogController.class);
@Autowired
private ILogService logService;
/**
* list
* @param reqParams reqParams
* @return
*/
@MyLog(type = "mytype")
@PostMapping("/list")
public RespResult list(@RequestBody LogReqParams reqParams) {
return logService.list(reqParams);
}
}