记录log+Aop mongon记录日志


@Aspect   //定义一个切面
@Configuration
public class LogAop {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    TokenHelper tokenHelper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    AllRoleService allRoleService;

    // 定义切点Pointcut
    @Pointcut("execution(* com.trvideo.controller.*.*.*(..))")
    public void excudeService() {
        System.out.println("时间:"+ this.getDate());
    }
	//切入内容环绕
    @Around("execution(* com.trvideo.controller.*.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        System.out.println("请求开始, 各个参数, url: "+url+", method: "+method+", uri: "+uri+", params: "+queryString);
        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
 		Gson gson = new Gson();
       //获取msg
        String msg = JSONObject.parseObject(gson.toJson(result)).getString("msg");
        //最后把 msg 存入mongondb
        //db.mongon.insert({msg:'xxxxxx'})
        return result;
    }


 public static String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        return time;
    }
}













你可能感兴趣的:(访问每个接口日志记录)