go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

logx support global fields?

Open charmfocus opened this issue 3 years ago • 1 comments
trafficstars

logx能否支持一下全局添加field功能,或默认加一个serviceName,收集日志想根据服务分索引很不方便

charmfocus avatar Sep 04 '22 09:09 charmfocus

add a middleware, that use ContextWithFields to inject it in ctx.

kevwan avatar Sep 17 '22 12:09 kevwan

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))
		}
	})

lhcGinv avatar Sep 22 '22 10:09 lhcGinv

I want it, too. Middleware is not convenient.

huiwanggo avatar Sep 22 '22 13:09 huiwanggo