WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments.
(attrs []slog.Attr)
| 197 | // WithAttrs returns a new Handler whose attributes consist of |
| 198 | // both the receiver's attributes and the arguments. |
| 199 | func (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. |