|
| 1 | +package slog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/samber/lo" |
| 6 | + "log/slog" |
| 7 | + "runtime" |
| 8 | + "slices" |
| 9 | +) |
| 10 | + |
| 11 | +var sourceKey = "source" |
| 12 | +var errorKeys = []string{"error", "err"} |
| 13 | + |
| 14 | +func convert(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) map[string]any { |
| 15 | + attrs := appendRecordAttrsToAttrs(loggerAttr, groups, record) |
| 16 | + |
| 17 | + attrs = replaceError(attrs, errorKeys...) |
| 18 | + if addSource { |
| 19 | + attrs = append(attrs, source(sourceKey, record)) |
| 20 | + } |
| 21 | + attrs = replaceAttrs(replaceAttr, []string{}, attrs...) |
| 22 | + attrs = removeEmptyAttrs(attrs) |
| 23 | + |
| 24 | + output := attrsToMap(attrs...) |
| 25 | + |
| 26 | + return output |
| 27 | +} |
| 28 | + |
| 29 | +type replaceAttrFn = func(groups []string, a slog.Attr) slog.Attr |
| 30 | + |
| 31 | +func appendRecordAttrsToAttrs(attrs []slog.Attr, groups []string, record *slog.Record) []slog.Attr { |
| 32 | + output := slices.Clone(attrs) |
| 33 | + |
| 34 | + slices.Reverse(groups) |
| 35 | + record.Attrs(func(attr slog.Attr) bool { |
| 36 | + for i := range groups { |
| 37 | + attr = slog.Group(groups[i], attr) |
| 38 | + } |
| 39 | + output = append(output, attr) |
| 40 | + return true |
| 41 | + }) |
| 42 | + |
| 43 | + return output |
| 44 | +} |
| 45 | + |
| 46 | +func replaceError(attrs []slog.Attr, errorKeys ...string) []slog.Attr { |
| 47 | + replaceAttr := func(groups []string, a slog.Attr) slog.Attr { |
| 48 | + if len(groups) > 1 { |
| 49 | + return a |
| 50 | + } |
| 51 | + |
| 52 | + for i := range errorKeys { |
| 53 | + if a.Key == errorKeys[i] { |
| 54 | + if err, ok := a.Value.Any().(error); ok { |
| 55 | + return slog.Any(a.Key, err.Error()) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return a |
| 60 | + } |
| 61 | + return replaceAttrs(replaceAttr, []string{}, attrs...) |
| 62 | +} |
| 63 | + |
| 64 | +func replaceAttrs(fn replaceAttrFn, groups []string, attrs ...slog.Attr) []slog.Attr { |
| 65 | + for i := range attrs { |
| 66 | + attr := attrs[i] |
| 67 | + value := attr.Value.Resolve() |
| 68 | + if value.Kind() == slog.KindGroup { |
| 69 | + attrs[i].Value = slog.GroupValue(replaceAttrs(fn, append(groups, attr.Key), value.Group()...)...) |
| 70 | + } else if fn != nil { |
| 71 | + attrs[i] = fn(groups, attr) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return attrs |
| 76 | +} |
| 77 | + |
| 78 | +func source(sourceKey string, r *slog.Record) slog.Attr { |
| 79 | + fs := runtime.CallersFrames([]uintptr{r.PC}) |
| 80 | + f, _ := fs.Next() |
| 81 | + var args []any |
| 82 | + if f.Function != "" { |
| 83 | + args = append(args, slog.String("function", f.Function)) |
| 84 | + } |
| 85 | + if f.File != "" { |
| 86 | + args = append(args, slog.String("file", f.File)) |
| 87 | + } |
| 88 | + if f.Line != 0 { |
| 89 | + args = append(args, slog.Int("line", f.Line)) |
| 90 | + } |
| 91 | + |
| 92 | + return slog.Group(sourceKey, args...) |
| 93 | +} |
| 94 | + |
| 95 | +func removeEmptyAttrs(attrs []slog.Attr) []slog.Attr { |
| 96 | + return lo.FilterMap(attrs, func(attr slog.Attr, _ int) (slog.Attr, bool) { |
| 97 | + if attr.Key == "" { |
| 98 | + return attr, false |
| 99 | + } |
| 100 | + |
| 101 | + if attr.Value.Kind() == slog.KindGroup { |
| 102 | + values := removeEmptyAttrs(attr.Value.Group()) |
| 103 | + if len(values) == 0 { |
| 104 | + return attr, false |
| 105 | + } |
| 106 | + |
| 107 | + attr.Value = slog.GroupValue(values...) |
| 108 | + return attr, true |
| 109 | + } |
| 110 | + |
| 111 | + return attr, !attr.Value.Equal(slog.Value{}) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func attrsToMap(attrs ...slog.Attr) map[string]any { |
| 116 | + output := map[string]any{} |
| 117 | + |
| 118 | + attrsByKey := groupValuesByKey(attrs) |
| 119 | + for k, values := range attrsByKey { |
| 120 | + v := mergeAttrValues(values...) |
| 121 | + if v.Kind() == slog.KindGroup { |
| 122 | + output[k] = attrsToMap(v.Group()...) |
| 123 | + } else { |
| 124 | + output[k] = v.Any() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return output |
| 129 | +} |
| 130 | + |
| 131 | +func groupValuesByKey(attrs []slog.Attr) map[string][]slog.Value { |
| 132 | + result := map[string][]slog.Value{} |
| 133 | + |
| 134 | + for _, item := range attrs { |
| 135 | + key := item.Key |
| 136 | + result[key] = append(result[key], item.Value) |
| 137 | + } |
| 138 | + |
| 139 | + return result |
| 140 | +} |
| 141 | + |
| 142 | +func mergeAttrValues(values ...slog.Value) slog.Value { |
| 143 | + v := values[0] |
| 144 | + |
| 145 | + for i := 1; i < len(values); i++ { |
| 146 | + if v.Kind() != slog.KindGroup || values[i].Kind() != slog.KindGroup { |
| 147 | + v = values[i] |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + v = slog.GroupValue(append(v.Group(), values[i].Group()...)...) |
| 152 | + } |
| 153 | + |
| 154 | + return v |
| 155 | +} |
| 156 | + |
| 157 | +func appendAttrsToGroup(groups []string, actualAttrs []slog.Attr, newAttrs ...slog.Attr) []slog.Attr { |
| 158 | + actualAttrs = slices.Clone(actualAttrs) |
| 159 | + |
| 160 | + if len(groups) == 0 { |
| 161 | + return uniqAttrs(append(actualAttrs, newAttrs...)) |
| 162 | + } |
| 163 | + |
| 164 | + for i := range actualAttrs { |
| 165 | + attr := actualAttrs[i] |
| 166 | + if attr.Key == groups[0] && attr.Value.Kind() == slog.KindGroup { |
| 167 | + actualAttrs[i] = slog.Group(groups[0], lo.ToAnySlice(appendAttrsToGroup(groups[1:], attr.Value.Group(), newAttrs...))...) |
| 168 | + return actualAttrs |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return uniqAttrs( |
| 173 | + append( |
| 174 | + actualAttrs, |
| 175 | + slog.Group( |
| 176 | + groups[0], |
| 177 | + lo.ToAnySlice(appendAttrsToGroup(groups[1:], []slog.Attr{}, newAttrs...))..., |
| 178 | + ), |
| 179 | + ), |
| 180 | + ) |
| 181 | +} |
| 182 | + |
| 183 | +func uniqAttrs(attrs []slog.Attr) []slog.Attr { |
| 184 | + return uniqByLast(attrs, func(item slog.Attr) string { |
| 185 | + return item.Key |
| 186 | + }) |
| 187 | +} |
| 188 | + |
| 189 | +func uniqByLast[T any, U comparable](collection []T, iteratee func(item T) U) []T { |
| 190 | + result := make([]T, 0, len(collection)) |
| 191 | + seen := make(map[U]int, len(collection)) |
| 192 | + seenIndex := 0 |
| 193 | + |
| 194 | + for _, item := range collection { |
| 195 | + key := iteratee(item) |
| 196 | + |
| 197 | + if index, ok := seen[key]; ok { |
| 198 | + result[index] = item |
| 199 | + continue |
| 200 | + } |
| 201 | + |
| 202 | + seen[key] = seenIndex |
| 203 | + seenIndex++ |
| 204 | + result = append(result, item) |
| 205 | + } |
| 206 | + |
| 207 | + return result |
| 208 | +} |
| 209 | + |
| 210 | +func contextExtractor(ctx context.Context, fns []func(ctx context.Context) []slog.Attr) []slog.Attr { |
| 211 | + var attrs []slog.Attr |
| 212 | + for _, fn := range fns { |
| 213 | + attrs = append(attrs, fn(ctx)...) |
| 214 | + } |
| 215 | + return attrs |
| 216 | +} |
0 commit comments