|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "sync" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + timeFormat = "[15:04:05.000]" |
| 15 | + logFormat = "%v\t%v\t%v" |
| 16 | + attrFormat = "%v=%v" |
| 17 | + attrSep = "\t" |
| 18 | + attrsFormat = "\t{%v}" |
| 19 | +) |
| 20 | + |
| 21 | +type CustomLogFileHandler struct { |
| 22 | + handler slog.Handler |
| 23 | + file io.Writer |
| 24 | + buf *bytes.Buffer |
| 25 | + mu *sync.Mutex |
| 26 | +} |
| 27 | + |
| 28 | +func (h *CustomLogFileHandler) Enabled(ctx context.Context, level slog.Level) bool { |
| 29 | + return h.handler.Enabled(ctx, level) |
| 30 | +} |
| 31 | + |
| 32 | +func (h *CustomLogFileHandler) WithAttrs(attrs []slog.Attr) slog.Handler { |
| 33 | + return &CustomLogFileHandler{handler: h.handler.WithAttrs(attrs), file: h.file, buf: h.buf, mu: h.mu} |
| 34 | +} |
| 35 | + |
| 36 | +func (h *CustomLogFileHandler) WithGroup(name string) slog.Handler { |
| 37 | + return &CustomLogFileHandler{handler: h.handler.WithGroup(name), file: h.file, buf: h.buf, mu: h.mu} |
| 38 | +} |
| 39 | + |
| 40 | +func (h *CustomLogFileHandler) Handle(ctx context.Context, r slog.Record) error { |
| 41 | + // Fetch attributes |
| 42 | + attrs, err := h.computeAttrs(ctx, r) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + attrLogs := "" |
| 48 | + for k, v := range attrs { |
| 49 | + if len(attrLogs) > 0 { |
| 50 | + attrLogs += attrSep |
| 51 | + } |
| 52 | + attrLogs += fmt.Sprintf(attrFormat, k, v) |
| 53 | + } |
| 54 | + |
| 55 | + logLine := fmt.Sprintf(logFormat, r.Time.Format(timeFormat), r.Level, r.Message) |
| 56 | + if len(attrLogs) > 0 { |
| 57 | + logLine += fmt.Sprintf(attrsFormat, attrLogs) |
| 58 | + } |
| 59 | + logLine += "\n" |
| 60 | + io.WriteString(h.file, logLine) |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (h *CustomLogFileHandler) computeAttrs(ctx context.Context, r slog.Record) (map[string]any, error) { |
| 66 | + h.mu.Lock() |
| 67 | + defer func() { |
| 68 | + h.buf.Reset() |
| 69 | + h.mu.Unlock() |
| 70 | + }() |
| 71 | + |
| 72 | + if err := h.handler.Handle(ctx, r); err != nil { |
| 73 | + return nil, fmt.Errorf("error when calling default handle: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + var attrs map[string]any |
| 77 | + err := json.Unmarshal(h.buf.Bytes(), &attrs) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("error when parsing default handle result: %w", err) |
| 80 | + } |
| 81 | + return attrs, nil |
| 82 | +} |
| 83 | + |
| 84 | +func NewCustomLogHandler(w io.Writer, opts *slog.HandlerOptions) *CustomLogFileHandler { |
| 85 | + if opts == nil { |
| 86 | + opts = &slog.HandlerOptions{} |
| 87 | + } |
| 88 | + |
| 89 | + b := &bytes.Buffer{} |
| 90 | + return &CustomLogFileHandler{ |
| 91 | + file: w, |
| 92 | + buf: b, |
| 93 | + handler: slog.NewJSONHandler(b, &slog.HandlerOptions{ |
| 94 | + Level: opts.Level, |
| 95 | + AddSource: opts.AddSource, |
| 96 | + ReplaceAttr: suppressDefaultAttrs(opts.ReplaceAttr), |
| 97 | + }), |
| 98 | + mu: &sync.Mutex{}, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Ignore the default log attributes |
| 103 | +func suppressDefaultAttrs(next func([]string, slog.Attr) slog.Attr) func([]string, slog.Attr) slog.Attr { |
| 104 | + return func(groups []string, a slog.Attr) slog.Attr { |
| 105 | + if a.Key == slog.TimeKey || |
| 106 | + a.Key == slog.LevelKey || |
| 107 | + a.Key == slog.MessageKey { |
| 108 | + return slog.Attr{} |
| 109 | + } |
| 110 | + if next == nil { |
| 111 | + return a |
| 112 | + } |
| 113 | + return next(groups, a) |
| 114 | + } |
| 115 | +} |
0 commit comments