日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java操作日志记录_通用日志记录(java)

發布時間:2024/7/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java操作日志记录_通用日志记录(java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*** 統一日志處理Handler

*@authorMingchenchen

**/

public classLogAopHandler {

@AutowiredprivateAuditLogDao auditLogDao;/*** controller層面記錄操作日志

* 注意此處是aop:around的 因為需要得到請求前的參數以及請求后接口返回的結果

*@throwsThrowable*/

public Object doSaveLog(ProceedingJoinPoint joinPoint) throwsThrowable {

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 instanceofString) {

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= newAuditLogEntity();

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(newDate());

auditLogDao.insert(auditLogEntity);

}

}

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java操作日志记录_通用日志记录(java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。