MCPcopy
hub / github.com/uber-go/zap / WithAttrs

Method WithAttrs

exp/zapslog/handler.go:199–220  ·  view source on GitHub ↗

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments.

(attrs []slog.Attr)

Source from the content-addressed store, hash-verified

197// WithAttrs returns a new Handler whose attributes consist of
198// both the receiver's attributes and the arguments.
199func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
200 fields := make([]zapcore.Field, 0, len(attrs)+len(h.groups))
201 var addedNamespace bool
202 for _, attr := range attrs {
203 f := convertAttrToField(attr)
204 if !addedNamespace && len(h.groups) > 0 && f != zap.Skip() {
205 // Namespaces are added only if at least one field is present
206 // to avoid creating empty groups.
207 fields = h.appendGroups(fields)
208 addedNamespace = true
209 }
210 fields = append(fields, f)
211 }
212
213 cloned := *h
214 cloned.core = h.core.With(fields)
215 if addedNamespace {
216 // These groups have been applied so we can clear them.
217 cloned.groups = nil
218 }
219 return &cloned
220}
221
222// WithGroup returns a new Handler with the given group appended to
223// the receiver's existing groups.

Callers 2

newSlogFunction · 0.80
newDisabledSlogFunction · 0.80

Calls 4

appendGroupsMethod · 0.95
SkipFunction · 0.92
convertAttrToFieldFunction · 0.85
WithMethod · 0.65

Tested by 2

newSlogFunction · 0.64
newDisabledSlogFunction · 0.64