diff --git a/api/middleware/reqinfo.go b/api/middleware/reqinfo.go index c4b518bd..3a0d0933 100644 --- a/api/middleware/reqinfo.go +++ b/api/middleware/reqinfo.go @@ -239,8 +239,10 @@ func AddBucketName(l *zap.Logger) Func { reqInfo := GetReqInfo(ctx) reqInfo.BucketName = chi.URLParam(r, BucketURLPrm) - reqLogger := reqLogOrDefault(ctx, l) - r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("bucket", reqInfo.BucketName)))) + if reqInfo.BucketName != "" { + reqLogger := reqLogOrDefault(ctx, l) + r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("bucket", reqInfo.BucketName)))) + } h.ServeHTTP(w, r) }) @@ -270,7 +272,9 @@ func AddObjectName(l *zap.Logger) Func { } } - r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("object", reqInfo.ObjectName)))) + if reqInfo.ObjectName != "" { + r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("object", reqInfo.ObjectName)))) + } h.ServeHTTP(w, r) }) diff --git a/api/middleware/response.go b/api/middleware/response.go index 7768ebd8..f458de0a 100644 --- a/api/middleware/response.go +++ b/api/middleware/response.go @@ -312,12 +312,20 @@ func LogSuccessResponse(l *zap.Logger) Func { reqLogger := reqLogOrDefault(ctx, l) reqInfo := GetReqInfo(ctx) - fields := []zap.Field{ - zap.String("method", reqInfo.API), - zap.String("bucket", reqInfo.BucketName), - zap.String("object", reqInfo.ObjectName), + fields := make([]zap.Field, 0, 6) + fields = append(fields, zap.Int("status", lw.statusCode), - zap.String("description", http.StatusText(lw.statusCode))} + zap.String("description", http.StatusText(lw.statusCode)), + zap.String("method", reqInfo.API), + ) + + if reqInfo.BucketName != "" { + fields = append(fields, zap.String("bucket", reqInfo.BucketName)) + } + if reqInfo.ObjectName != "" { + fields = append(fields, zap.String("object", reqInfo.ObjectName)) + } + if traceID, err := trace.TraceIDFromHex(reqInfo.TraceID); err == nil && traceID.IsValid() { fields = append(fields, zap.String("trace_id", reqInfo.TraceID)) }