您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了springboot怎么利用@Aspect实现日志工具类的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springboot怎么利用@Aspect实现日志工具类文章都会有所收获,下面我们一起来看看吧。
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
@SpringBootApplication @EnableCaching @EnableAsync @ComponentScan({"com.workorder.*"}) public class WorkOrderApplication { public static void main(String[] args) { SpringApplication.run(WorkOrderApplication.class, args); } }
@Aspect @Component @Slf4j public class LogAspect { @Autowired private Environment env; @Pointcut("execution(public * com.workorder..controller..*.*(..))") public void logPointCut() {} @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { log.info("========================================="); long beginTime = System.currentTimeMillis(); //执行方法 Object result = point.proceed(); //执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; //保存日志 saveSysLog(point, time); return result; } private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String logStr=""; if (isEnvironment("dev")){ String requestUrl = request.getRequestURL().toString() + "?"; //获取所有参数方法 Enumeration<String> enu=request.getParameterNames(); while(enu.hasMoreElements()){ String paraName = enu.nextElement(); requestUrl+=paraName + "=" + request.getParameter(paraName) + "&"; } logStr+="\r\n请求地址:"+ requestUrl.substring(0,requestUrl.length()-1); } //请求的参数 Object[] args = joinPoint.getArgs(); String params = JSONUtil.toJsonStr(args); logStr+="\r\n请求方式:"+ request.getMethod(); logStr+="\r\n请求方法Token:"+ request.getHeader("token"); logStr+="\r\n请求参数:"+ params; logStr+="\r\nIP:"+ request.getRemoteAddr(); logStr+="\r\n响应时间:"+ time+" ms"; log.info(logStr); /** * 判断当前是什么环境 * @param str * @return */ private boolean isEnvironment(String str) { boolean flag = false; String[] arr = env.getActiveProfiles(); List<String> list = Arrays.asList(arr); if (list.contains(str)) { flag = true; } return flag; } }
关于“springboot怎么利用@Aspect实现日志工具类”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“springboot怎么利用@Aspect实现日志工具类”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。