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

Function convertAttrToField

exp/zapslog/handler.go:76–114  ·  exp/zapslog/handler.go::convertAttrToField
(attr slog.Attr)

Source from the content-addressed store, hash-verified

74}
75
76func convertAttrToField(attr slog.Attr) zapcore.Field {
77 if attr.Equal(slog.Attr{}) {
78 // Ignore empty attrs.
79 return zap.Skip()
80 }
81
82 switch attr.Value.Kind() {
83 case slog.KindBool:
84 return zap.Bool(attr.Key, attr.Value.Bool())
85 case slog.KindDuration:
86 return zap.Duration(attr.Key, attr.Value.Duration())
87 case slog.KindFloat64:
88 return zap.Float64(attr.Key, attr.Value.Float64())
89 case slog.KindInt64:
90 return zap.Int64(attr.Key, attr.Value.Int64())
91 case slog.KindString:
92 return zap.String(attr.Key, attr.Value.String())
93 case slog.KindTime:
94 return zap.Time(attr.Key, attr.Value.Time())
95 case slog.KindUint64:
96 return zap.Uint64(attr.Key, attr.Value.Uint64())
97 case slog.KindGroup:
98 if attr.Key == "" {
99 // Inlines recursively.
100 return zap.Inline(groupObject(attr.Value.Group()))
101 }
102 return zap.Object(attr.Key, groupObject(attr.Value.Group()))
103 case slog.KindLogValuer:
104 return convertAttrToField(slog.Attr{
105 Key: attr.Key,
106 // TODO: resolve the value in a lazy way.
107 // This probably needs a new Zap field type
108 // that can be resolved lazily.
109 Value: attr.Value.Resolve(),
110 })
111 default:
112 return zap.Any(attr.Key, attr.Value.Any())
113 }
114}
115
116// convertSlogLevel maps slog Levels to zap Levels.
117// Note that there is some room between slog levels while zap levels are continuous, so we can't 1:1 map them.

Callers 3

MarshalLogObjectMethod · 0.85
HandleMethod · 0.85
WithAttrsMethod · 0.85

Calls 14

SkipFunction · 0.92
BoolFunction · 0.92
DurationFunction · 0.92
Float64Function · 0.92
Int64Function · 0.92
StringFunction · 0.92
TimeFunction · 0.92
Uint64Function · 0.92
InlineFunction · 0.92
ObjectFunction · 0.92
AnyFunction · 0.92
groupObjectTypeAlias · 0.85

Tested by

no test coverage detected