go-zero
go-zero copied to clipboard
logx support global fields?
trafficstars
logx能否支持一下全局添加field功能,或默认加一个serviceName,收集日志想根据服务分索引很不方便
add a middleware, that use ContextWithFields to inject it in ctx.
You can use middleware:
server.Use(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
serviceNameField := logx.LogField{
Key: "serviceName",
Value: "xxx",
}
otherField := logx.LogField{
Key: "other",
Value: xxx",
}
// ...
ctx = logx.WithFields(ctx, serviceNameField, otherField)
next(w, r.WithContext(ctx))
}
})
I want it, too. Middleware is not convenient.