MCPcopy
hub / github.com/rs/zerolog / appendSlogAttr

Function appendSlogAttr

slog.go:179–244  ·  view source on GitHub ↗

appendSlogAttr appends a single slog.Attr to the zerolog event, handling type-specific encoding to avoid reflection where possible.

(event *Event, attr slog.Attr, prefix string)

Source from the content-addressed store, hash-verified

177// appendSlogAttr appends a single slog.Attr to the zerolog event, handling
178// type-specific encoding to avoid reflection where possible.
179func appendSlogAttr(event *Event, attr slog.Attr, prefix string) *Event {
180 if event == nil {
181 return event
182 }
183
184 // Resolve the attribute to handle LogValuer types.
185 // This handles slog.KindLogValuer implicitly by unwrapping
186 // any values that implement slog.LogValuer to their resolved form.
187 attr.Value = attr.Value.Resolve()
188
189 // For group kinds, handle grouping before key concatenation
190 if attr.Value.Kind() == slog.KindGroup {
191 attrs := attr.Value.Group()
192 if len(attrs) == 0 {
193 return event
194 }
195 groupPrefix := joinPrefix(prefix, attr.Key)
196 for _, ga := range attrs {
197 event = appendSlogAttr(event, ga, groupPrefix)
198 }
199 return event
200 }
201
202 // Skip empty keys for non-group attributes
203 if attr.Key == "" {
204 return event
205 }
206
207 key := joinPrefix(prefix, attr.Key)
208 val := attr.Value
209
210 switch val.Kind() {
211 case slog.KindString:
212 event = event.Str(key, val.String())
213 case slog.KindInt64:
214 event = event.Int64(key, val.Int64())
215 case slog.KindUint64:
216 event = event.Uint64(key, val.Uint64())
217 case slog.KindFloat64:
218 event = event.Float64(key, val.Float64())
219 case slog.KindBool:
220 event = event.Bool(key, val.Bool())
221 case slog.KindDuration:
222 event = event.Dur(key, val.Duration())
223 case slog.KindTime:
224 event = event.Time(key, val.Time())
225 case slog.KindAny:
226 v := val.Any()
227 switch cv := v.(type) {
228 case error:
229 event = event.AnErr(key, cv)
230 case time.Duration:
231 event = event.Dur(key, cv)
232 case time.Time:
233 event = event.Time(key, cv)
234 case []byte:
235 event = event.Bytes(key, cv)
236 default:

Callers 1

HandleMethod · 0.85

Calls 13

joinPrefixFunction · 0.85
StrMethod · 0.45
StringMethod · 0.45
Int64Method · 0.45
Uint64Method · 0.45
Float64Method · 0.45
BoolMethod · 0.45
DurMethod · 0.45
TimeMethod · 0.45
AnyMethod · 0.45
AnErrMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected