由于项目使用了Graphql,当需要定位问题时就少不了查找日志,为了方便让请求直接在postman中使用,就做了Graphql 请求的aop 拦截并且格式化输出
package service.aop;
import com.google.gson.Gson;
import graphql.kickstart.execution.GraphQLObjectMapper;
import graphql.kickstart.execution.GraphQLRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect
public class GraphqlLogPrintAspect {
@Autowired
GraphQLObjectMapper objectMapper;
@Pointcut("execution(* graphql.kickstart.spring.AbstractGraphQLController.*(..))")
public void pointCut() {
}
@Before("pointCut()")
public void beforePrint(JoinPoint joinPoint) {
try {
String body = (String) joinPoint.getArgs()[4];
GraphQLRequest request = objectMapper.readGraphQLRequest(body);
log.info("Graphql rquest->>:{}", new Gson().toJson(request).toString());
} catch (Exception e) {
log.error("doPrintLog graphql error:", e);
}
}
}
效果:
本人公众号 会不定期推送一些文章 ,有兴趣可以关注