系统运行后,可以通过/ObjectLogger/log/query查询系统中记录的日志,并通过传入参数对日志进行过滤。
通过这里,我们可以查询下一步中写入的日志。
5 日志写入
业务系统在任何需要进行日志记录的类中引入LogClient。例如:
- @Autowired
- private LogClient logClient;
5.1 简单使用
直接将对象的零个、一个、多个属性变化放入actionItemModelList中发出即可。actionItemModelList置为null则表示此次对象无需要记录的属性变动。例如,业务应用中调用:
- logClient.sendLogForItems("TaskModel",5,"actor name","addTask","add Task","via web page","some comments",null);
在ObjectLogger中使用如下查询条件:
- http://{your_ObjectLogger_address}/ObjectLogger/log/query?appName=myBootApp&objectName=TaskModel&objectId=5
查询到日志:
- {
- "respMsg": "成功",
- "respData": [
- {
- "id": 16,
- "appName": "myBootApp",
- "objectName": "TaskModel",
- "objectId": 5,
- "actor": "actor name",
- "action": "addTask",
- "actionName": "add Task",
- "extraWords": "via web page",
- "comment": "some comments",
- "actionTime": "2019-04-10T10:56:15.000+0000",
- "actionItemModelList": []
- }
- ],
- "respCode": "1000"
- }
5.2 对象变动自动记录
该功能可以自动完成新老对象的对比,并根据对比结果,将多个属性变动一起写入日志系统中。使用时,要确保传入的新老对象属于同一个类。
例如,业务系统这样调用:
- TaskModel oldTaskModel = new TaskModel();
- oldTaskModel.setId(9);
- oldTaskModel.setTaskName("oldName");
- oldTaskModel.setUserId(3);
- oldTaskModel.setDescription(" <p>the first line</p>
- " +
- " <p>the second line</p>
- " +
- " <p>the 3th line</p>");
- TaskModel newTaskModel = new TaskModel();
- newTaskModel.setId(9);
- newTaskModel.setTaskName("newName");
- newTaskModel.setUserId(5);
- newTaskModel.setDescription(" <p>the first line</p>
- " +
- " <p>the second line</p>
- " +
- " <p>the last line</p>");
- logClient.sendLogForObject(9,"actor name","editTask","edit Task","via app",
- "some comments",oldTaskModel,newTaskModel);
则我们可以使用下面查询条件:
- http://{your_ObjectLogger_address}/ObjectLogger/log/query?appName=myBootApp&objectName=TaskModel&objectId=9
查询到如下结果:
- {
- "respMsg": "成功",
- "respData": [
- {
- "id": 15,
- "appName": "myBootApp",
- "objectName": "TaskModel",
- "objectId": 9,
- "actor": "actor name",
- "action": "editTask",
- "actionName": "edit Task",
- "extraWords": "via app",
- "comment": "some comments",
- "actionTime": "2019-04-10T10:56:17.000+0000",
- "actionItemModelList": [
- {
- "id": 18,
- "actionId": 15,
- "attributeType": "NORMAL",
- "attribute": "taskName",
- "attributeName": "TASK",
- "oldValue": "oldName",
- "newValue": "newName",
- "diffValue": null
- },
- {
- "id": 19,
- "actionId": 15,
- "attributeType": "USERID",
- "attribute": "userId",
- "attributeName": "USER",
- "oldValue": "USER:3",
- "newValue": "USER:5",
- "diffValue": "diffValue"
- },
- {
- "id": 20,
- "actionId": 15,
- "attributeType": "TEXT",
- "attribute": "description",
- "attributeName": "DESCRIPTION",
- "oldValue": ""\t<p>the first line</p>\n\t<p>the second line</p>\n\t<p>the 3th line</p>"",
- "newValue": ""\t<p>the first line</p>\n\t<p>the second line</p>\n\t<p>the last line</p>"",
- "diffValue": "第6行变化:<br/> -:<del> the 3th line </del> <br/> +:<u> the last line </u> <br/>"
- }
- ]
- }
- ],
- "respCode": "1000"
- }
6 对象属性过滤
有些对象的属性的变动不需要进行日志记录,例如updateTime、hashCode等。ObjectLogger支持对对象的属性进行过滤,只追踪我们感兴趣的属性。
并且,对于每个属性我们可以更改其记录到ObjectLogger系统中的具体方式,例如修改命名等。
要想启用这个功能,首先将配置中的object.logger.autoLog改为false。
- object.logger.autoLog=false
然后在需要进行变化日志记录的属性上增加@LogTag注解。凡是没有增加该注解的属性在日志记录时会被自动跳过。
(编辑:ASP站长网)
|