update slog/logger #351

Merged
vtolstov merged 11 commits from devstigneev/micro:fix_attrs_logger into v3 2024-10-12 12:37:44 +03:00
Showing only changes of commit c1e3e9c5a2 - Show all commits

View File

@ -224,11 +224,12 @@ func (l *lWrapper) Call(ctx context.Context, req client.Request, rsp interface{}
return err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ClientCallObservers {
labels = append(labels, o(ctx, req, rsp, opts, err)...)
attrs := convertToInterface(o(ctx, req, rsp, opts, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return err
}
@ -247,11 +248,12 @@ func (l *lWrapper) Stream(ctx context.Context, req client.Request, opts ...clien
return stream, err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ClientStreamObservers {
labels = append(labels, o(ctx, req, opts, stream, err)...)
attrs := convertToInterface(o(ctx, req, opts, stream, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return stream, err
}
@ -270,11 +272,12 @@ func (l *lWrapper) Publish(ctx context.Context, msg client.Message, opts ...clie
return err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ClientPublishObservers {
labels = append(labels, o(ctx, msg, opts, err)...)
attrs := convertToInterface(o(ctx, msg, opts, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return err
}
@ -293,11 +296,12 @@ func (l *lWrapper) ServerHandler(ctx context.Context, req server.Request, rsp in
return err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ServerHandlerObservers {
labels = append(labels, o(ctx, req, rsp, err)...)
attrs := convertToInterface(o(ctx, req, rsp, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return err
}
@ -316,11 +320,12 @@ func (l *lWrapper) ServerSubscriber(ctx context.Context, msg server.Message) err
return err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ServerSubscriberObservers {
labels = append(labels, o(ctx, msg, err)...)
attrs := convertToInterface(o(ctx, msg, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return err
}
@ -363,11 +368,12 @@ func (l *lWrapper) ClientCallFunc(ctx context.Context, addr string, req client.R
return err
}
var labels []string
var labels []interface{}
for _, o := range l.opts.ClientCallFuncObservers {
labels = append(labels, o(ctx, addr, req, rsp, opts, err)...)
attrs := convertToInterface(o(ctx, addr, req, rsp, opts, err))
labels = append(labels, attrs...)
}
l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level)
l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level)
return err
}
@ -397,3 +403,13 @@ func NewServerSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
return l.ServerSubscriber
}
}
func convertToInterface(attrs []string) []interface{} {
result := make([]interface{}, 0, len(attrs))
for idx := range attrs {
result = append(result, attrs[idx])
}
return result
}