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: https://github.com/elltor/oplog

Preview renderings:

oplog

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

characteristic

  • 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

preface

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 = "com.elltor.biz", mode = AdviceMode.PROXY)
@SpringBootApplication
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).

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

3. User defined log operation

Implement interface 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. 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:

@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. 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

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

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