1 Star 0 Fork 1

疯三侠 / ObjectLogger

forked from 苌文松 / ObjectLogger 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0
ObjectLogger

ObjectLogger

language version codebeat badge license

强大且易用的Java对象日志记录系统,支持对象属性变化的自动分析、记录、查询。


English Introduction


1 系统简介

ObjectLogger是一套强大且易用的对象日志记录系统。它能够将任意对象的变动日志记录下来,并支持查询。可以应用在用户操作日志记录、对象属性变更记录等诸多场景中。

该系统具有以下特点:

  • 一站整合:系统支持日志的记录与查询,开发者只需再开发前端界面即可使用。
  • 完全独立:与业务系统无耦合,可插拔使用,不影响主业务流程。
  • 应用共享:系统可以同时供多个业务系统使用,互不影响。
  • 简单易用:服务端直接jar包启动;业务系统有官方Maven插件支持。
  • 自动解析:能自动解析对象的属性变化,并支持富文本的前后对比。
  • 便于扩展:支持自定义对象变动说明、属性变动说明。支持更多对象属性类型的扩展。

整个项目包含四个部分:

  • ObjectLoggerClient:能够集成到业务系统进行日志分析、发送jar包。可以从Maven官方仓库引入该jar包。该模块位于client子包下。
  • ObjectLoggerServer:一个web服务,需要数据库的支持。它能够接收并保存ObjectLoggerClient发出的日志信息,支持日志的查询操作。该模块位于server子包下。
  • react-object-logger:一个React前端组件,用于进行日志的前端展示。可以从npm官方仓库引入该组件。该子项目位于react-object-logger
  • ObjectLoggerDemo:一个业务端集成ObjectLoggerClient的示例。该模块位于demo子包下。

2 快速上手

2.1 创建数据库

使用该项目的/server/database/init_data_table.sql文件初始化两个数据表。

2.2 启动Server

下载该项目下最新的Server服务jar包,地址为/server/target/ObjectLoggerServer-*.jar

启动下载的jar包。

java -jar ObjectLoggerServer-*.jar --spring.datasource.driver-class-name={db_driver} --spring.datasource.url=jdbc:{db}://{db_address}/{db_name} --spring.datasource.username={db_username} --spring.datasource.password={db_password}

上述命令中的用户配置项说明如下:

  • db_driver:你所用的数据库对应的驱动。如果使用MySql数据库则为com.mysql.jdbc.Driver;如果使用SqlServer数据库则为com.microsoft.sqlserver.jdbc.SQLServerDriver
  • db:数据库类型。如果使用MySql数据库则为mysql;如果使用SqlServer数据库则为sqlserver
  • db_address:数据库连接地址。如果数据库在本机则为127.0.0.1
  • db_name:数据库名,该数据库中需包含上一步初始化的两个数据表。
  • db_username:数据库登录用户名。
  • db_password:数据库登录密码。

启动jar成功后可以看到下面的界面:

使用浏览器访问下面的页面可以看到系统欢迎页面:

http://127.0.0.1:12301/ObjectLoggerServer/

访问上述地址可以看到下面的欢迎界面:

至此,ObjectLoggerServer系统已经搭建结束,可以接受业务系统的日志写入和查询操作。

3 业务系统接入

该部分讲解如何配置业务系统来将业务系统中的对象变化通过ObjectLoggerClient分析,然后记录到ObjectLoggerServer中。

这一部分的使用可以参照ObjectLoggerDemo项目,该项目给出了业务系统集成ObjectLoggerClient的详细示例。ObjectLoggerDemo的制品包可以从/demo/target/ObjectLoggerDemo-*.jar获得,无需其他配置直接运行java -jar ObjectLoggerDemo-*.jar便可以直接启动该项目。

可以直接根据启动页面的提示访问相关地址来体验:

3.1 引入依赖包

在pom中增加下面的依赖:

<dependency>
    <groupId>com.github.yeecode.objectlogger</groupId>
    <artifactId>ObjectLoggerClient</artifactId>
    <version>{最新版本}</version>
</dependency>

3.2 添加对ObjectLoggerClient中bean的自动注入

3.2.1 对于SpringBoot应用

在SpringBoot的启动类前添加@ComponentScan注解,并在basePackages中增加ObjectLoggerClient的包地址:com.github.yeecode.objectlogger,如:

@SpringBootApplication
@ComponentScan(basePackages={"{your_beans_root}","com.github.yeecode.objectlogger"})
public class MyBootAppApplication {
public static void main(String[] args) {
    // 省略其他代码
  }
}

3.2.2 对于Spring应用

applicationContext.xml增加对ObjectLoggerClient包地址的扫描:

<context:component-scan base-package="com.github.yeecode.objectlogger">
</context:component-scan>

3.3 完成配置

application.properties中增加:

yeecode.objectLogger.serverAddress=http://{ObjectLoggerServer_address}
yeecode.objectLogger.businessAppName={your_app_name}
yeecode.objectLogger.autoLogAttributes=true
  • ObjectLoggerServer_address:属性指向上一步的ObjectLoggerServer的部署地址,例如:127.0.0.1:12301
  • your_app_name:指当前业务系统的应用名。以便于区分日志来源,实现同时支持多个业务系统
  • yeecode.objectLogger.autoLogAttributes:是否对对象的所有属性进行变更日志记录

至此,业务系统的配置完成。已经实现了和ObjectLoggerServer端的对接。

4 日志查询

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

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

5 日志展示

ObjectLogger有前端React组件react-object-logger支持,用于进行日志信息的展示。体验页面:react-object-logger 示例页面

展示效果如图:

感兴趣的用户可以前往react-object-logger进行了解。

同时也欢迎各位开发者开发面向其它前端技术栈的组件。

6 日志写入

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

@Autowired
private LogClient logClient;

6.1 简单使用

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

logClient.logAttributes(
                "CleanRoomTask",
                5,
                "Tom",
                "add",
                "Add New Task",
                "Create a cleanRoomTask",
                "taskName is :Demo Task",
                null);

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

http://127.0.0.1:12301/ObjectLoggerServer/log/query?appName=ObjectLoggerDemo&objectName=CleanRoomTask&objectId=5

查询到日志:

{
  "respMsg": "SUCCESS",
  "respData": [
    {
      "id": 1,
      "appName": "ObjectLoggerDemo",
      "objectName": "CleanRoomTask",
      "objectId": 5,
      "operator": "Jone",
      "operationName": "start",
      "operationAlias": "Start a Task",
      "extraWords": "Begin to clean room...",
      "comment": "Come on and start cleaning up.",
      "operationTime": "2019-07-04T06:53:40.000+0000",
      "attributeModelList": [
        {
          "attributeType": "NORMAL",
          "attributeName": "status",
          "attributeAlias": "Status",
          "oldValue": "TODO",
          "newValue": "DOING",
          "diffValue": null,
          "id": 1,
          "operationId": 1
        }
      ]
    }
  ],
  "respCode": "1000"
}

6.2 对象变动自动记录

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

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

CleanRoomTask task = new CleanRoomTask();
task.setId(5);
task.setTaskName("Demo Task");
task.setStatus("TODO");
task.setDescription("Do something...");

CleanRoomTask oldTask = logClient.deepCopy(task);

task.setId(5);
task.setTaskName("Demo Task");
task.setStatus("DOING");
task.setDescription("The main job is to clean the floor.");
task.setAddress("Sunny Street");
task.setRoomNumber(702);

logClient.logObject(
                cleanRoomTask.getId().toString(),
                "Tom",
                "update",
                "Update a Task",
                null,
                null,
                oldTask,
                task);

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

http://127.0.0.1:12301/ObjectLoggerServer/log/query?appName=ObjectLoggerDemo&objectName=CleanRoomTask&objectId=5

查询到如下结果:

{
  "respMsg": "SUCCESS",
  "respData": [
    {
      "id": 4,
      "appName": "ObjectLoggerDemo",
      "objectName": "CleanRoomTask",
      "objectId": 5,
      "operator": "Tom",
      "operationName": "update",
      "operationAlias": "Update a Task",
      "extraWords": null,
      "comment": null,
      "operationTime": "2019-07-04T07:22:59.000+0000",
      "attributeModelList": [
        {
          "attributeType": "NORMAL",
          "attributeName": "roomNumber",
          "attributeAlias": "roomNumber",
          "oldValue": "",
          "newValue": "702",
          "diffValue": null,
          "id": 5,
          "operationId": 4
        },
        {
          "attributeType": "NORMAL",
          "attributeName": "address",
          "attributeAlias": "address",
          "oldValue": "",
          "newValue": "Sunny Street",
          "diffValue": null,
          "id": 6,
          "operationId": 4
        },
        {
          "attributeType": "NORMAL",
          "attributeName": "status",
          "attributeAlias": "Status",
          "oldValue": "TODO",
          "newValue": "DOING",
          "diffValue": null,
          "id": 7,
          "operationId": 4
        },
        {
          "attributeType": "TEXT",
          "attributeName": "description",
          "attributeAlias": "Description",
          "oldValue": "Do something...",
          "newValue": "The main job is to clean the floor.",
          "diffValue": "Line 1<br/>&nbsp;&nbsp;&nbsp; -: <del> Do something... </del> <br/>&nbsp;&nbsp; +: <u> The main job is to clean the floor. </u> <br/>",
          "id": 8,
          "operationId": 4
        }
      ]
    }
  ],
  "respCode": "1000"
}

7 对象属性过滤

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

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

要想启用这个功能,首先将配置中的yeecode.objectLogger.autoLogAttributes改为false

yeecode.objectLogger.autoLogAttributes=true

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

例如,注解配置如下则id字段的变动将被忽略。

private Integer id;

@LogTag
private String taskName;

@LogTag(alias = "UserId", extendedType = "userIdType")
private int userId;

@LogTag(alias = "Status")
private String status;

@LogTag(alias = "Description", builtinType = BuiltinTypeHandler.TEXT)
private String description;

该注解属性介绍如下:

  • alias:属性别名。默认情况下会将属性名写入。
  • builtinType:ObjectLoggerClient的内置类型,为BuiltinTypeHandler的值。默认为BuiltinTypeHandler.NORMAL
    • BuiltinTypeHandler.NORMAL:记录属性的新值和旧值,对比值为null
    • BuiltinTypeHandler.RICHTEXT: 用户富文本对比。记录属性值的新值和旧值,并将新旧值转化为纯文本后逐行对比差异,对比值中记录差异
  • extendedType:扩展属性类型。使用ObjcetLogger时,用户可以扩展某些字段的处理方式,此时,alias等信息均可以被用户自主覆盖。

8 属性处理扩展

很多情况下,用户希望能够自主决定某些对象属性的处理方式。例如,对于例子中Task对象的userId属性,用户可能想将其转化为姓名后存入日志系统,从而使得日志系统与userId完全解耦。

ObjectLoggerClient完全支持这种情况,可以让用户自主决定某些属性的日志记录方式。要想实现这种功能,首先在需要进行扩展处理的属性上为@LogTagextendedType属性赋予一个字符串值。例如:

@LogTag(alias = "UserId", extendedType = "userIdType")
private int userId;

然后在业务系统中声明一个Bean继承BaseExtendedTypeHandler,作为自由扩展的钩子。代码如下:

@Service
public class ExtendedTypeHandler implements BaseExtendedTypeHandler {
    @Override
    public BaseAttributeModel handleAttributeChange(String extendedType, String attributeName, String attributeAlias, Object oldValue, Object newValue) {
        // TODO
    }
}

接下来,当ObjectLoggerClient处理到该属性时,会将该属性的相关信息传入到扩展Bean的handleAttributeChange方法中,然后用户可以自行处理。传入的四个参数解释如下:

  • extendedType:扩展类型值,即@LogTag注解的extendedType值。本示例中为userIdType
  • attributeName:属性名。本示例中为userId
  • attributeAlias:属性别名,@LogTag注解的alias值。本示例中为UserId
  • oldValue:该属性的旧值。
  • newValue:该属性的新值。

例如我们可以采用如下的方式处理userIdType属性:

@Service
public class ExtendedTypeHandler implements BaseExtendedTypeHandler {
    @Override
    public BaseAttributeModel handleAttributeChange(String extendedType, String attributeName, String attributeAlias, Object oldValue, Object newValue) {
        BaseAttributeModel baseAttributeModel = new BaseAttributeModel();
        if (extendedType.equals("userIdType")) {
            // 这里仅作示例,实际使用中可以进行调用用户系统将userId转化为userName的操作等
            baseAttributeModel.setOldValue("USER_" + oldValue);
            baseAttributeModel.setNewValue("USER_" + newValue);
            baseAttributeModel.setDiffValue(oldValue + "->" + newValue);
        }
        return baseAttributeModel;
    }
}

9 Roadmap

  • 3.1.1:增加对象深度拷贝功能,便于用户保存变更前的对象
  • 3.0.1: 优化系统命名,对比值输出转为灵活的json
  • 3.0.0:优化系统命名
  • 2.3.0:增加对继承属性的自动记录功能
  • 2.2.0:增加全局对象属性变动自动记录功能
  • 2.0.1:日志写入支持多线程
  • 2.0.0:重新组织代码结构
  • 1.0.0:实现基本功能
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

A powerful and easy-to-use operational logging system that supports analysis of changes in object properties. 强大且易用的操作日志记录系统,支持对象属性的变化分析。 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/CrazySanxia/ObjectLogger.git
git@gitee.com:CrazySanxia/ObjectLogger.git
CrazySanxia
ObjectLogger
ObjectLogger
master

搜索帮助