From 9dfb23eb6d8fecba95e26e1ce999a1e6463fc32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20S=C3=B6derlund?= Date: Mon, 21 Oct 2024 12:46:38 +0200 Subject: [PATCH] feat(cloudrequestlog): migrate AdditionalFields.AppendTo to slog BREAKING CHANGE: Migrated from zap.Field to slog.Attr. --- cloudrequestlog/additionalfields.go | 23 +---------------------- cloudrequestlog/middleware.go | 6 +++--- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/cloudrequestlog/additionalfields.go b/cloudrequestlog/additionalfields.go index 2e3c531d..9a472957 100644 --- a/cloudrequestlog/additionalfields.go +++ b/cloudrequestlog/additionalfields.go @@ -60,17 +60,7 @@ func (m *AdditionalFields) AddToArray(key string, objects ...any) { } // AppendTo appends the additional fields to the input fields. -func (m *AdditionalFields) AppendTo(fields []zap.Field) []zap.Field { - m.mu.Lock() - fields = append(fields, m.fields...) - for _, array := range m.arrays { - fields = append(fields, zap.Array(array.key, anyArray(array.values))) - } - m.mu.Unlock() - return fields -} - -func (m *AdditionalFields) appendTo(attrs []slog.Attr) []slog.Attr { +func (m *AdditionalFields) AppendTo(attrs []slog.Attr) []slog.Attr { m.mu.Lock() defer m.mu.Unlock() for _, field := range m.fields { @@ -82,17 +72,6 @@ func (m *AdditionalFields) appendTo(attrs []slog.Attr) []slog.Attr { return attrs } -type anyArray []any - -func (oa anyArray) MarshalLogArray(encoder zapcore.ArrayEncoder) error { - for _, o := range oa { - if err := encoder.AppendReflected(o); err != nil { - return err - } - } - return nil -} - func argsToFieldSlice(args []any) []zap.Field { var attr slog.Attr fields := make([]zap.Field, 0, len(args)) diff --git a/cloudrequestlog/middleware.go b/cloudrequestlog/middleware.go index f31beb37..7d30faee 100644 --- a/cloudrequestlog/middleware.go +++ b/cloudrequestlog/middleware.go @@ -61,7 +61,7 @@ func (l *Middleware) GRPCUnaryServerInterceptor( } attrs = appendFullMethodAttrs(info.FullMethod, attrs) if additionalFields, ok := GetAdditionalFields(ctx); ok { - attrs = additionalFields.appendTo(attrs) + attrs = additionalFields.AppendTo(attrs) } var errCaller interface { Caller() (pc uintptr, file string, line int, ok bool) @@ -106,7 +106,7 @@ func (l *Middleware) GRPCStreamServerInterceptor( } attrs = appendFullMethodAttrs(info.FullMethod, attrs) if additionalFields, ok := GetAdditionalFields(ctx); ok { - attrs = additionalFields.appendTo(attrs) + attrs = additionalFields.AppendTo(attrs) } var errCaller interface { Caller() (pc uintptr, file string, line int, ok bool) @@ -216,7 +216,7 @@ func (l *Middleware) HTTPServer(next http.Handler) http.Handler { slog.Any("httpRequest", &httpRequest), } if additionalFields, ok := GetAdditionalFields(ctx); ok { - attrs = additionalFields.appendTo(attrs) + attrs = additionalFields.AppendTo(attrs) } logger.LogAttrs(ctx, level, logMessage, attrs...) })