操作日志注解(装饰器)。组装 Event 后通过反向注册的 Recorder 落库——pkg 不直接依赖 internal/system,落库时机由 internal/system 在启动时把 OperLogSvcApp.RecordOper 注入。这是 pkg 反向依赖 internal 的少数例外之一。
关键导出
Event struct{Title, BusinessType, Method, OperName, OperParam, JSONResult, Status, ErrorMsg, CostTime, ...}Recorder func(ctx context.Context, evt *Event)Init(r Recorder)反向注册(须在database.Init之后)Log(title string, businessType enum.BusinessType, opts ...Option) gin.HandlerFunc(须排在鉴权之后)Option:WithOperatorType / WithoutRequestData / WithoutResponseData / WithExcludeParams
典型用法
go
// cmd/standalone/main.go:63
oplog.Init(systemservice.OperLogSvcApp.RecordOper)
// internal/system/router.go:61
profile.PUT("/update",
oplog.Log(profileLogTitle, enum.BusinessTypeUpdate),
repeatsubmit.RepeatSubmit(0, ""),
handler.ProfileApiApp.UpdateProfile)坑/约定
WARNING
Init必须传 Recorder(nil panic);未注册时 emit 只打日志不阻断业务。OperParam在 handler 之前采集(body 是一次性流,须配middleware.RepeatableBody)。- 响应体边写边抄(封顶
maxCaptureBytes,非 JSON 响应如 xlsx 不抄)。 - emit 用
context.WithoutCancel——响应完原 ctx 即取消,否则异步落库必失败。 - 各字段按
sys_oper_log列宽 rune 截断。 :::
相关页:/crud-guide/router、/pkg-reference/middleware、/modules/system。