A util for operating log. Easy to use.

Related tags

Spring Boot oplog
Overview

oplog

一个便于使用的操作日志工具。

语言 : [ 英语 | 中文 ]

功能

  • 通过表达式解析生成美观的表达式,支持解析入参、自定义变量、返回值(_ret)、错误信息(_errMsg)
  • 可以通过条件控制是否记录日志
  • 提供自定义函数扩展
  • 提供自定获取上下文用户扩展
  • 支持嵌套、支持多线程使用情景

使用和效果预览:

/**
 * 示例 1 - 基本使用
 * 记录订单编号和返回值信息。
 *
 * 通过日志持久化实现类 {@link com.elltor.example.config.log.PersistenceLogServiceImpl } 打印结果
 * 通过 {@link com.elltor.example.config.log.LogRecordOperatorGetImpl } 自动获取操作用户
 * 通过自定义函数 {@link com.elltor.example.config.log.OrderDetailParseFunction } 解析订单详细信息
 */
// 打印日志注解
@LogRecord(
        // 必填,方法执行成功生成的模板(执行过程未捕获到异常), orderDetail为自定义函数名
        success = "订单详细信息: {#orderDetail(#id)}, 执行状态码:{#_ret.status}",
        // 可选,执行失败的模板(捕捉到错误)
        fail = "获取订单失败 id: {#id}, 执行状态码: {#_ret.status}",
        // 可选,业务id
        bizNo = "{#id}",
        // 可选,日志的分类
        category = LogType.ORDER)
@ApiOperation("获取订单信息")
@GetMapping("/{id}")
public Object getOrderById(@PathVariable("id") Long id)throws Exception{
        // 模拟成功失败
        Result success=new Result("success",200,orderService.getOrderById(id));
        Result fail=new Result("ERROR",500,orderService.getOrderById(id));

        // 随机成功失败
        return new Random().nextInt(10)>=5?success:fail;
        }

解析 @LogRecord 注解后的效果 :

{
  "success": "订单详细信息: 【 id : 1001订单名称:男士卫衣一件 地址:北京市海淀区 】, 执行状态码:200",
  "fail": "获取订单失败 id: 1001, 执行状态码: 200",
  "operator": "zhangsan13",
  "bizNo": "1001",
  "category": "order",
  "detail": "",
  "condition": "true",
  "complete": true,
  "timestamp": 1648710916438
}

特性

  • 便于使用。 提供 starter, 通过注解开启功能并自动配置。

  • 灵活。 工具提供了自定义函数、具有表达式解析的日志模版和注解层面的日志持久化控制。

  • 可扩展。 提供了自定义函数、自定义日志操作用户、自定义持久化日志方式。

  • 性能。 通过异步和缓存的方式提升处理性能。

业务逻辑架构

业务逻辑

使用方法

前言

oplog-example 是一个演示模块,你可以通过这个项目快速了解工具的使用和功能。 启动项目后点击链接 http://localhost:8080/swagger-ui/index.html 访问 swagger 文档就可以尝试功能了。

1. 开始使用

开启日志记录功能 :

@EnableLogRecord(tenant = "com.elltor.biz", mode = AdviceMode.PROXY)
@SpringBootApplication
public class Application {
    //.....
}

添加注解 @LogRecord 到你要记录日志的方法上。

被注解注释的方方法的入参和返回值将被作为日志模版的上下文。

    @LogRecord(success = "查询了用户, 用户id : {#userid}", category = "user")
public Object getUserByUserid(String userid){
        return new Object();
        }

好了,到这里你就可以使用基本的功能了。

2. 自定义日志持久化方式

基础父类 AbstractLogRecordService 并且实现持久化方法 record(Record record)

@Component
@Slf4j
public class PersistenceLogServiceImpl extends AbstractLogRecordService {
    @Override
    public void record(Record record) {
        log.info("example 包中 : {}", record);
    }
}

3. 自定义日志操作用户

实现接口 IOperatorGetService :

@Component
public class LogRecordOperatorGetImpl implements IOperatorGetService {
    @Override
    public Operator getUser() {
        User user = ContextHolder.currentUser();
        Operator op = new Operator();
        op.setUsername(user.getUsername());
        op.setName(user.getName());
        return op;
    }
}

4. 自定义解析函数

注意:

  • 自定义的解析函数必须是静态方法. 如下面的 userDetail 方法。
  • 返回值总是 String 类型的。

实现接口 IParseFunction :

@Component
public class UserDetailFunction implements IParseFunction {

    @Component
    private UserDao userDao;

    @Override
    public Method functionMethod() {
        Method method = null;
        try {
            method = UserDetailFunction.class.getDeclaredMethod("userDetail", String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return method;
    }

    // custom function
    static String userDetail(String userid) {
        User u = userDao.getUserByUserid(userid);
        return u.getName() + " " + u.getSex() + " " + u.getAge();
    }
}

5. 使用约定

在注解 LogRecord 中的字段 :

  • success : 成功方法模版,必填字段。
  • condition : 是否记录日志。值为boolean值, 但类型为 String
  • operator : 总是通过实现 IOperatorGetService 接口的类获取或者由你指定。
  • fail : 用来记录方法执行失败的模版文案。

使用技术

  • SpEL(Spring Expression Language)
  • Spring Message Service
  • Java Annotation
  • Spring Boot Auto-configuration

实现参考

https://tech.meituan.com/2021/09/16/operational-logbook.html

You might also like...

Extremely simple and easy to use lucky blocks plugin!

Extremely simple and easy to use lucky blocks plugin!

SimpleLuckyBlocks plugin, the new fully customisable lucky blocks plugin Ever felt the need for a free, yet simple lucky blocks plugin? Well you've fo

Jun 8, 2022

A React Native project starter with Typescript, a theme provider with hook to easy styling component, a folder architecture ready and some configs to keep a codebase clean.

React Native Boilerplate Folder structure : src ├── assets │   ├── audios │   ├── fonts │   ├── icons │   └── images ├── components │   ├── Layout.tsx

Sep 1, 2022

A networking library for LibGDX utilizing Netty allowing easy creation of multiplayer games.

Lunar What is Lunar? Lunar is a networking library for LibGDX. With lunar you can easily create multiplayer games quickly and efficiently. Lunar provi

Nov 25, 2022

Fast Android Development. Easy maintainance.

Fast Android Development. Easy maintenance. AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the pl

Dec 31, 2022

A simple Plugin to allow server admins and user with Permissions to change fast and easy thier own Gamemode

A simple Plugin to allow server admins and user with Permissions to change fast and easy thier own Gamemode

A simple Plugin to allow server admins and user with Permissions to change fast and easy thier own Gamemode

Jan 17, 2022

Simple Minecraft mod that makes it easy to put horses in boats.

Simple Minecraft mod that makes it easy to put horses in boats.

HorseInBoat What does this mod do? This mod makes it much easier to put horses into boats. This mod also changes the hitbox of horses in boats, this i

Dec 20, 2022

JHusky - Modern native Git hooks made easy for java environments

JHusky Modern native Git hooks made easy for java environments JHusky improves your commits and more 🐶 Jwoof! Install Include it to your project as a

Oct 31, 2022

🐀 Simple, Fast and easy to implement ORM for most popular databases

RatORM Simple, Fast and easy to implement ORM for most popular databases Status: Branch Tests Code Quality master Usefull links Helpful links: GitHub

Dec 25, 2022

This repository contains Java programs to become zero to hero in Java. Programs related to each and every concep are present from easy to intermidiate level.

Learn Java Programming In this repository you will find topic wise programs of java from basics to intermediate. This follows topic wise approach that

Oct 9, 2022
Comments
  • 重复解析

    重复解析

    https://github.com/elltor/oplog/blob/bf75ad3ccd21b5dc76f69a19da138fbb5215a4dc/oplog-core/src/main/java/com/elltor/oplog/aspect/LogRecordAspect.java#L220 你好,想问一下这里为什么需要将 oldTemplate 重新解析一次?

    opened by tomchan2015 1
Owner
Qichun Liu
Enjoy coooooooooding
Qichun Liu
Java-Trading-Log-Project - A Trading Log to Journal Your Trades.

Abhi's Project - Trading Log Trading Background I am very passionate about trading. I have been studying the financial markets for a few years and hav

Abhigyan Dabla 0 Jul 18, 2022
A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use

lock-spring-boot-starter A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use 一款基于 Redis 和 Zookeeper

Pear Stack 9 Oct 15, 2022
Minecraft configurable plugin , which sends messages the first time a player logs into the server or the next time they log in.

JoinMessages Minecraft configurable plugin , which sends messages the first time a player logs into the server or the next time they log in or leave.

ᴠᴀʟᴇɴᴛɪɴ ᴢʜᴇʟᴇᴠ 6 Aug 30, 2022
Manages server status and gives log of status information. front end - angular js, backend- sbring boot, DB-MySQL

ServerManagerApplication / | | / | | ( ___ _ __ __ __ ___ _ __ | \ / | __ _ _ __ __ _ __ _ ___ _ __ __ \ / _ \ | '| \ \ / / / _ \ | '| | |/| | / | | '

null 1 Jan 6, 2022
GalaxyCDC is a core component of PolarDB-X which is responsible for global binary log generation, publication and subscription.

中文文档 What is ApsaraDB GalaxyCDC ? GalaxyCDC is a core component of PolarDB-X which is responsible for global binary log generation, publication and su

null 56 Dec 19, 2022
Simple Design for Java bridge with Javascript. Also can get javascript console.log.

SDBridgeKotlin is here. If your h5 partner confused about how to deal with iOS and Android. This Demo maybe help. 最常见的问题. WebViewJavascriptBridge is n

null 25 Dec 18, 2022
Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

geekidea 2.3k Dec 31, 2022
GreenMail is an open source, intuitive and easy-to-use test suite of email servers for testing purposes.

GreenMail GreenMail is an open source, intuitive and easy-to-use test suite of email servers for testing purposes. Supports SMTP, POP3, IMAP with SSL

null 529 Dec 28, 2022
An easy-to-use Spigot library that implements a GUI interface within offhand maps.

MiniMapGUI An easy-to-use Spigot library that implements a GUI interface within offhand maps. Maven/Gradle Dependencies Replace Tag with the latest ve

byteful 10 Oct 17, 2022
A simple and easy to use personal medical notes

a-medic-log A simple and easy to use personal medical notes. Easily add your medical notes Multiple profile to keep track elderly and family members m

null 8 Oct 9, 2022