~james/core

core/log/log.go -rw-r--r-- 8.9 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package log

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"os"

	"github.com/charmbracelet/log"
	"go.opentelemetry.io/contrib/bridges/otelslog"
	otellog "go.opentelemetry.io/otel/log"
	"go.opentelemetry.io/otel/trace"
)

type handlerOperation struct {
	attrs []slog.Attr
	group string
}

type namedHandler struct {
	slog.Handler
	name       string
	format     string
	writer     io.Writer
	hasTraceID bool
	hasSpanID  bool
	operations []handlerOperation
}

func (h *namedHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	hasTraceID := h.hasTraceID
	hasSpanID := h.hasSpanID
	for _, attr := range attrs {
		switch attr.Key {
		case "trace_id":
			hasTraceID = true
		case "span_id":
			hasSpanID = true
		}
	}
	operations := append(h.operations[:len(h.operations):len(h.operations)], handlerOperation{
		attrs: append([]slog.Attr(nil), attrs...),
	})
	return &namedHandler{
		Handler:    h.Handler.WithAttrs(attrs),
		name:       h.name,
		format:     h.format,
		writer:     h.writer,
		hasTraceID: hasTraceID,
		hasSpanID:  hasSpanID,
		operations: operations,
	}
}

func (h *namedHandler) WithGroup(name string) slog.Handler {
	operations := append(h.operations[:len(h.operations):len(h.operations)], handlerOperation{group: name})
	return &namedHandler{
		Handler:    h.Handler.WithGroup(name),
		name:       h.name,
		format:     h.format,
		writer:     h.writer,
		hasTraceID: h.hasTraceID,
		hasSpanID:  h.hasSpanID,
		operations: operations,
	}
}

func (h *namedHandler) WithName(name string) slog.Handler {
	next, err := newHandler(h.writer, name, h.format)
	if err != nil {
		return h
	}
	for _, operation := range h.operations {
		if operation.group != "" {
			next = next.WithGroup(operation.group)
		} else {
			next = next.WithAttrs(operation.attrs)
		}
	}
	return next
}

func (h *namedHandler) Handle(ctx context.Context, r slog.Record) error {
	spanContext := trace.SpanContextFromContext(ctx)
	if spanContext.IsValid() && spanContext.IsSampled() {
		hasTraceID := h.hasTraceID
		hasSpanID := h.hasSpanID
		if !hasTraceID || !hasSpanID {
			r.Attrs(func(a slog.Attr) bool {
				if a.Key == "trace_id" {
					hasTraceID = true
				}
				if a.Key == "span_id" {
					hasSpanID = true
				}
				return !hasTraceID || !hasSpanID
			})
		}
		var attrs []slog.Attr
		if !hasTraceID {
			attrs = append(attrs, slog.String("trace_id", spanContext.TraceID().String()))
		}
		if !hasSpanID {
			attrs = append(attrs, slog.String("span_id", spanContext.SpanID().String()))
		}
		if len(attrs) > 0 {
			r.AddAttrs(attrs...)
		}
	}
	return h.Handler.Handle(ctx, r)
}

type fanoutHandler struct {
	primary slog.Handler
	others  []slog.Handler
}

func (h *fanoutHandler) Enabled(ctx context.Context, level slog.Level) bool {
	if h.primary.Enabled(ctx, level) {
		return true
	}
	for _, other := range h.others {
		if other.Enabled(ctx, level) {
			return true
		}
	}
	return false
}

func (h *fanoutHandler) Handle(ctx context.Context, r slog.Record) error {
	var errs []error
	if h.primary.Enabled(ctx, r.Level) {
		if err := h.primary.Handle(ctx, r); err != nil {
			errs = append(errs, err)
		}
	}
	for _, other := range h.others {
		if other.Enabled(ctx, r.Level) {
			if err := other.Handle(ctx, r); err != nil {
				errs = append(errs, err)
			}
		}
	}
	if len(errs) > 0 {
		return errors.Join(errs...)
	}
	return nil
}

func (h *fanoutHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	others := make([]slog.Handler, len(h.others))
	for i, other := range h.others {
		others[i] = other.WithAttrs(attrs)
	}
	return &fanoutHandler{
		primary: h.primary.WithAttrs(attrs),
		others:  others,
	}
}

func (h *fanoutHandler) WithGroup(name string) slog.Handler {
	others := make([]slog.Handler, len(h.others))
	for i, other := range h.others {
		others[i] = other.WithGroup(name)
	}
	return &fanoutHandler{
		primary: h.primary.WithGroup(name),
		others:  others,
	}
}

func (h *fanoutHandler) WithName(name string) slog.Handler {
	newPrimary := h.primary
	if nh, ok := h.primary.(interface{ WithName(string) slog.Handler }); ok {
		newPrimary = nh.WithName(name)
	}
	others := make([]slog.Handler, len(h.others))
	for i, other := range h.others {
		if nh, ok := other.(interface{ WithName(string) slog.Handler }); ok {
			others[i] = nh.WithName(name)
		} else {
			others[i] = other
		}
	}
	return &fanoutHandler{
		primary: newPrimary,
		others:  others,
	}
}

func NewFanoutHandler(primary slog.Handler, others ...slog.Handler) slog.Handler {
	var validOthers []slog.Handler
	for _, other := range others {
		if other != nil {
			validOthers = append(validOthers, other)
		}
	}
	if len(validOthers) == 0 {
		return primary
	}
	return &fanoutHandler{
		primary: primary,
		others:  validOthers,
	}
}

type otelHandler struct {
	slog.Handler
	provider   otellog.LoggerProvider
	name       string
	operations []handlerOperation
}

func (h *otelHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	operations := append(h.operations[:len(h.operations):len(h.operations)], handlerOperation{
		attrs: append([]slog.Attr(nil), attrs...),
	})
	return &otelHandler{
		Handler:    h.Handler.WithAttrs(attrs),
		provider:   h.provider,
		name:       h.name,
		operations: operations,
	}
}

func (h *otelHandler) WithGroup(name string) slog.Handler {
	operations := append(h.operations[:len(h.operations):len(h.operations)], handlerOperation{group: name})
	return &otelHandler{
		Handler:    h.Handler.WithGroup(name),
		provider:   h.provider,
		name:       h.name,
		operations: operations,
	}
}

func (h *otelHandler) WithName(newName string) slog.Handler {
	var opts []otelslog.Option
	if h.provider != nil {
		opts = append(opts, otelslog.WithLoggerProvider(h.provider))
	}
	var handler slog.Handler = otelslog.NewHandler(newName, opts...)
	for _, operation := range h.operations {
		if operation.group != "" {
			handler = handler.WithGroup(operation.group)
		} else {
			handler = handler.WithAttrs(operation.attrs)
		}
	}
	return &otelHandler{
		Handler:    handler,
		provider:   h.provider,
		name:       newName,
		operations: h.operations,
	}
}

func NewOtelHandler(provider otellog.LoggerProvider, name string) slog.Handler {
	var opts []otelslog.Option
	if provider != nil {
		opts = append(opts, otelslog.WithLoggerProvider(provider))
	}
	h := otelslog.NewHandler(name, opts...)
	return &otelHandler{
		Handler:  h,
		provider: provider,
		name:     name,
	}
}

func newHandler(w io.Writer, name, format string) (slog.Handler, error) {
	var handler slog.Handler
	switch format {
	case "", "text":
		handler = log.NewWithOptions(w, log.Options{
			ReportTimestamp: true,
			Prefix:          name,
			Level:           log.DebugLevel,
		})
		format = "text"
	case "json":
		handler = slog.NewJSONHandler(w, &slog.HandlerOptions{
			Level: slog.LevelDebug,
		}).WithAttrs([]slog.Attr{slog.String("logger", name)})
	default:
		return nil, fmt.Errorf("unknown log format %q", format)
	}
	return &namedHandler{
		Handler: handler,
		name:    name,
		format:  format,
		writer:  w,
	}, nil
}

func NewHandler(name string) slog.Handler {
	handler, _ := newHandler(os.Stderr, name, "text")
	return handler
}

func New(name string) *slog.Logger {
	return slog.New(NewHandler(name))
}

func NewWithFormat(name, format string) (*slog.Logger, error) {
	return newWithFormatWriter(os.Stderr, name, format)
}

func newWithFormatWriter(w io.Writer, name, format string) (*slog.Logger, error) {
	handler, err := newHandler(w, name, format)
	if err != nil {
		return nil, err
	}
	return slog.New(handler), nil
}

func NewContext(ctx context.Context, name string) context.Context {
	return IntoContext(ctx, New(name))
}

type ctxKey struct{}

// IntoContext adds a logger to a context. Use FromContext to
// pull the logger out.
func IntoContext(ctx context.Context, l *slog.Logger) context.Context {
	return context.WithValue(ctx, ctxKey{}, l)
}

// FromContext returns a logger from a context.Context;
// if the passed context is nil, we return the default slog
// logger.
func FromContext(ctx context.Context) *slog.Logger {
	if ctx != nil {
		v := ctx.Value(ctxKey{})
		if v == nil {
			return slog.Default()
		}
		return v.(*slog.Logger)
	}

	return slog.Default()
}

// sublogger derives a new logger from an existing one by appending a suffix to its prefix.
func SubLogger(base *slog.Logger, suffix string) *slog.Logger {
	if base == nil {
		return New(suffix)
	}
	if handler, ok := base.Handler().(*namedHandler); ok {
		name := suffix
		if handler.name != "" {
			name = handler.name + "/" + suffix
		}
		return slog.New(handler.WithName(name))
	}
	if fh, ok := base.Handler().(*fanoutHandler); ok {
		var newName string
		if ph, ok := fh.primary.(*namedHandler); ok {
			if ph.name != "" {
				newName = ph.name + "/" + suffix
			} else {
				newName = suffix
			}
		} else {
			newName = suffix
		}
		return slog.New(fh.WithName(newName))
	}
	if handler, ok := base.Handler().(*log.Logger); ok {
		name := suffix
		if handler.GetPrefix() != "" {
			name = handler.GetPrefix() + "/" + suffix
		}
		return New(name)
	}
	return base.With("component", suffix)
}