| 74 | } |
| 75 | |
| 76 | func 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. |