Open source operation log tool

Keywords: Java Back-end

Absrtact: it is inevitable to record sensitive operations in business systems, which can be realized by hard coding or simple annotation, but they are not elegant enough. Today, I share a log tool written by myself.

This is a logging tool implemented with reference to an article by meituan. It can record operation logs flexibly and has good scalability. Welcome to try it. The following is the introduction and tutorial of this tool.

Code warehouse:

Preview renderings:


oplog is an easy-to-use operation logging tool.


  • Easy to use. It provides a starter, which enables the function through annotation and is automatically configured.
  • Flexible. The tool provides user-defined functions, log templates with expression parsing, and log persistence control at the annotation level.
  • Scalable. It provides user-defined functions, user-defined log operations, and user-defined persistent log methods.
  • Performance. Improve processing performance through asynchronous and caching.

Business logic architecture

usage method


Oplog example is a demonstration module. You can quickly understand the use and functions of the tool through this project. Click the link after starting the project http://localhost:8080/swagger-ui/index.html access the swagger document to try out the functionality.

1. Start using

Enable logging:

@EnableLogRecord(tenant = "", mode = AdviceMode.PROXY)
public class Application {

Add the annotation @ LogRecord to the method you want to log.

The input parameter and return value of the annotated method will be used as the context of the log template.

@LogRecord(success = "User queried, user id : {#userid}", category = "user")
    public Object getUserByUserid(String userid) {
        return new Object();

Well, here you can use the basic functions.

2. Customize log persistence method

The basic parent class AbstractLogRecordService and implements the persistent method record(Record record).

public class PersistenceLogServiceImpl extends AbstractLogRecordService {
    public void record(Record record) {"example Package : {}", record);

3. User defined log operation

Implement interface IOperatorGetService:

public class LogRecordOperatorGetImpl implements IOperatorGetService {
    public Operator getUser() {
        User user = ContextHolder.currentUser();
        Operator op = new Operator();
        return op;

4. User defined parsing function

be careful:

  • The custom parsing function must be a static method, such as the userDetail method below.
  • The return value is always of type String.

Implement interface IParseFunction:

public class UserDetailFunction implements IParseFunction {
    private UserDao userDao;
    public Method functionMethod() {
        Method method = null;
            method = UserDetailFunction.class.getDeclaredMethod("userDetail", String.class);
        }catch (NoSuchMethodException e){
        return method;
    // custom function
    static String userDetail(String userid){
        User u = userDao.getUserByUserid(userid);
        return u.getName()+" "+u.getSex()+" "+u.getAge();

5. Usage agreement

Fields in the annotation LogRecord:

  • Success: success method template, required field.
  • condition: whether to log. The value is a boolean value, but the type is String.
  • operator: always obtained through the class that implements the IOperatorGetService interface or specified by you.
  • fail: template file used to record method execution failures.

Use technology

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

Implementation reference

Posted by kevingarnett2000 on Thu, 18 Nov 2021 07:43:19 -0800