今天就跟大家聊聊有关JAVA项目中怎么实现一个通用日志记录功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。使用Aop记录操作日志添加aoppublic class LogAopHa
今天就跟大家聊聊有关JAVA项目中怎么实现一个通用日志记录功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
使用Aop记录操作日志
添加aop
public class LogAopHandler { @Autowired private AuditLogDao auditLogDao; public Object doSaveLog(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature method = (MethodSignature) joinPoint.getSignature(); String methodName = method.getName(); Object[] objects = joinPoint.getArgs(); String requestBody = null; if (objects!=null && objects.length>0) { for (Object object : objects) { if (object == null) { requestBody = null;//POST接口参数为空 比如删除XXX }else if (object instanceof String) { requestBody = (String) object;//有些接口直接把参数转换成对象了 }else { requestBody = JSONObject.tojsONString(object); } } } //只记录POST方法的日志 boolean isNeedSaveLog = false; //此处不能用getAnnotationByType 是JAVA8的特性,因为注解能够重名,所以得到的是数组 RequestMapping annotation = method.getMethod().getAnnotation(RequestMapping.class); for (RequestMethod requestMethod : annotation.method()) { if (requestMethod==RequestMethod.POST) { isNeedSaveLog = true; } } JSONObject requestBodyJson = null; try { requestBodyJson = JSONObject.parseObject(requestBody); } catch (Exception e) { //do nothing 即POST请求没传body } httpservletRequest request = RequestContextUtil.getRequestByCurrentContext(); String userName = RequestContextUtil.getUserNameByCurrentContext(); if (StringUtil.isEmpty(userName)) { try { userName = DmsCache.get(requestBodyJson.getString("userName")).getName(); } catch (Exception e) { userName = RequestContextUtil.getAsynUserInfoByAutoDeploy().getName(); } } //得到request的参数后让方法执行它 //注意around的情况下需要返回result 否则将不会返回值给请求者 Object result = joinPoint.proceed(objects); try { JSONObject resultJson = JSONObject.parseObject(result.toString()); if (isNeedSaveLog) {//如果是POST请求 则记录日志 LogTypeEnum logTypeEnum = LogTypeEnum.getDesByMethodName(methodName); if (logTypeEnum != null) { AuditLogEntity auditLogEntity = new AuditLogEntity(); auditLogEntity.setUuid(StringUtil.createRandomUuid()); auditLogEntity.setOperator(userName); auditLogEntity.setRequestIp(request.getRemoteAddr()); auditLogEntity.setRequestUrl(request.getRequestURI().replace("/cloud-master", "")); auditLogEntity.setEventType(logTypeEnum.geTKEy()); auditLogEntity.setEventDesc(logTypeEnum.getDescription()); auditLogEntity.setRequest(requestBody); int isSuccess = "200".equals(resultJson.getString("code")) ? 1 : 0; auditLogEntity.setSuccessFlag(isSuccess); auditLogEntity.setResponse(result.toString()); auditLogEntity.setCreateTime(new Date()); auditLogDao.insert(auditLogEntity); } } } catch (Exception e) { e.printStackTrace(); } return result; } }
--结束END--
本文标题: JAVA项目中怎么实现一个通用日志记录功能
本文链接: https://www.lsjlt.com/news/225735.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0