Skip to content

Commit

Permalink
feat(cloudrequestlog): migrate AdditionalFields.AppendTo to slog
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Migrated from zap.Field to slog.Attr.
  • Loading branch information
odsod committed Oct 21, 2024
1 parent d834237 commit 9dfb23e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
23 changes: 1 addition & 22 deletions cloudrequestlog/additionalfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions cloudrequestlog/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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...)
})
Expand Down

0 comments on commit 9dfb23e

Please sign in to comment.