加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

架构师必看!操作日志系统搭建秘技

发布时间:2019-07-04 13:53:11 所属栏目:Windows 来源:程序员进阶架构师
导读:副标题#e# 在Java开发中,我们经常会遇到一个棘手的问题:记录用户的操作行为。 某些操作是相对简单的,我们可以逐条记录。但是某些操作行为却很难记录,例如编辑操作。在某一次操作中,用户可能编辑了对象A的几个属性,而下一次操作中用户可能编辑了对象B

系统运行后,可以通过/ObjectLogger/log/query查询系统中记录的日志,并通过传入参数对日志进行过滤。

架构师必看!操作日志系统搭建秘技

通过这里,我们可以查询下一步中写入的日志。

5 日志写入

业务系统在任何需要进行日志记录的类中引入LogClient。例如:

  1. @Autowired 
  2. private LogClient logClient; 

5.1 简单使用

直接将对象的零个、一个、多个属性变化放入actionItemModelList中发出即可。actionItemModelList置为null则表示此次对象无需要记录的属性变动。例如,业务应用中调用:

  1. logClient.sendLogForItems("TaskModel",5,"actor name","addTask","add Task","via web page","some comments",null); 

在ObjectLogger中使用如下查询条件:

  1. http://{your_ObjectLogger_address}/ObjectLogger/log/query?appName=myBootApp&objectName=TaskModel&objectId=5 

查询到日志:

  1.  "respMsg": "成功", 
  2.  "respData": [ 
  3.  { 
  4.  "id": 16, 
  5.  "appName": "myBootApp", 
  6.  "objectName": "TaskModel", 
  7.  "objectId": 5, 
  8.  "actor": "actor name", 
  9.  "action": "addTask", 
  10.  "actionName": "add Task", 
  11.  "extraWords": "via web page", 
  12.  "comment": "some comments", 
  13.  "actionTime": "2019-04-10T10:56:15.000+0000", 
  14.  "actionItemModelList": [] 
  15.  } 
  16.  ], 
  17.  "respCode": "1000" 

5.2 对象变动自动记录

该功能可以自动完成新老对象的对比,并根据对比结果,将多个属性变动一起写入日志系统中。使用时,要确保传入的新老对象属于同一个类。

例如,业务系统这样调用:

  1. TaskModel oldTaskModel = new TaskModel(); 
  2. oldTaskModel.setId(9); 
  3. oldTaskModel.setTaskName("oldName"); 
  4. oldTaskModel.setUserId(3); 
  5. oldTaskModel.setDescription("   <p>the first line</p> 
  6. " + 
  7.  "  <p>the second line</p> 
  8. " + 
  9.  "  <p>the 3th line</p>"); 
  10. TaskModel newTaskModel = new TaskModel(); 
  11. newTaskModel.setId(9); 
  12. newTaskModel.setTaskName("newName"); 
  13. newTaskModel.setUserId(5); 
  14. newTaskModel.setDescription("   <p>the first line</p> 
  15. " + 
  16.  "  <p>the second line</p> 
  17. " + 
  18.  "  <p>the last line</p>"); 
  19. logClient.sendLogForObject(9,"actor name","editTask","edit Task","via app", 
  20. "some comments",oldTaskModel,newTaskModel); 

则我们可以使用下面查询条件:

  1. http://{your_ObjectLogger_address}/ObjectLogger/log/query?appName=myBootApp&objectName=TaskModel&objectId=9 

查询到如下结果:

  1.  "respMsg": "成功", 
  2.  "respData": [ 
  3.  { 
  4.  "id": 15, 
  5.  "appName": "myBootApp", 
  6.  "objectName": "TaskModel", 
  7.  "objectId": 9, 
  8.  "actor": "actor name", 
  9.  "action": "editTask", 
  10.  "actionName": "edit Task", 
  11.  "extraWords": "via app", 
  12.  "comment": "some comments", 
  13.  "actionTime": "2019-04-10T10:56:17.000+0000", 
  14.  "actionItemModelList": [ 
  15.  { 
  16.  "id": 18, 
  17.  "actionId": 15, 
  18.  "attributeType": "NORMAL", 
  19.  "attribute": "taskName", 
  20.  "attributeName": "TASK", 
  21.  "oldValue": "oldName", 
  22.  "newValue": "newName", 
  23.  "diffValue": null 
  24.  }, 
  25.  { 
  26.  "id": 19, 
  27.  "actionId": 15, 
  28.  "attributeType": "USERID", 
  29.  "attribute": "userId", 
  30.  "attributeName": "USER", 
  31.  "oldValue": "USER:3", 
  32.  "newValue": "USER:5", 
  33.  "diffValue": "diffValue" 
  34.  }, 
  35.  { 
  36.  "id": 20, 
  37.  "actionId": 15, 
  38.  "attributeType": "TEXT", 
  39.  "attribute": "description", 
  40.  "attributeName": "DESCRIPTION", 
  41.  "oldValue": ""t<p>the first line</p>nt<p>the second line</p>nt<p>the 3th line</p>"", 
  42.  "newValue": ""t<p>the first line</p>nt<p>the second line</p>nt<p>the last line</p>"", 
  43.  "diffValue": "第6行变化:<br/> -:<del> the 3th line </del> <br/> +:<u> the last line </u> <br/>" 
  44.  } 
  45.  ] 
  46.  } 
  47.  ], 
  48.  "respCode": "1000" 

6 对象属性过滤

有些对象的属性的变动不需要进行日志记录,例如updateTime、hashCode等。ObjectLogger支持对对象的属性进行过滤,只追踪我们感兴趣的属性。

并且,对于每个属性我们可以更改其记录到ObjectLogger系统中的具体方式,例如修改命名等。

要想启用这个功能,首先将配置中的object.logger.autoLog改为false。

  1. object.logger.autoLog=false 

然后在需要进行变化日志记录的属性上增加@LogTag注解。凡是没有增加该注解的属性在日志记录时会被自动跳过。

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读