myzhan / boomer

A better load generator for locust, written in golang.
MIT License
1.18k stars 242 forks source link

把 Boomer 结构体的字段 logger 改成 interface 是否更合适一些呢 #204

Open alexshopee opened 1 month ago

alexshopee commented 1 month ago

目前 Boomer 结构体的定义如下:

type Boomer struct {
    masterHost  string
    masterPort  int
    mode        Mode
    rateLimiter RateLimiter
    slaveRunner *slaveRunner

    localRunner *localRunner
    spawnCount  int
    spawnRate   float64

    cpuProfileFile     string
    cpuProfileDuration time.Duration

    memoryProfileFile     string
    memoryProfileDuration time.Duration

    outputs []Output

    logger *log.Logger
}

其中 logger 字段是 golang 标准库的 Logger 结构体,这样用户只能传一个初始化 Logger 的变量作为 Boomer 的 log,如果改成 interface,定义类似于:

    Logger interface {
        Debug(msg string, keyvals ...interface{})
        Info(msg string, keyvals ...interface{})
        Warn(msg string, keyvals ...interface{})
        Error(msg string, keyvals ...interface{})
    }

的方式,用户可以更灵活,代价更小地接入业务代码中实际需要的 log 呢,有可能是 zap.Logger 等等,如果用户不指定的话再传给一个默认的 Logger ,也许这个方案更灵活一些呢,目前很多的库都是类似的思路,例如 machinery,temporal 等

myzhan commented 4 weeks ago

确实,从设计上,这样更灵活。但是当时增加这个 logger,只是为了打印 boomer 内部的一些日志,一般情况下,都不需要日志打印。