|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/fatih/color" |
| 14 | +) |
| 15 | + |
| 16 | +// Handler is a colored slog handler. |
| 17 | +type PrettyHandler struct { |
| 18 | + groups []string |
| 19 | + attrs []slog.Attr |
| 20 | + |
| 21 | + opts slog.HandlerOptions |
| 22 | + |
| 23 | + mu *sync.Mutex |
| 24 | + out io.Writer |
| 25 | +} |
| 26 | + |
| 27 | +var LevelTags = map[slog.Level]string{ |
| 28 | + slog.LevelDebug: color.New(color.FgWhite, color.Bold).Sprint("DEBUG"), |
| 29 | + slog.LevelInfo: color.New(color.FgBlue, color.Bold).Sprint("INFO"), |
| 30 | + slog.LevelWarn: color.New(color.FgYellow, color.Bold).Sprint("WARN"), |
| 31 | + slog.LevelError: color.New(color.FgRed, color.Bold).Sprint("ERROR"), |
| 32 | +} |
| 33 | + |
| 34 | +// NewHandler creates a new [Handler] with the specified options. If opts is nil, uses [DefaultOptions]. |
| 35 | +func NewPrettyHandler(out io.Writer, opts *slog.HandlerOptions) *PrettyHandler { |
| 36 | + h := &PrettyHandler{out: out, mu: &sync.Mutex{}} |
| 37 | + if opts == nil { |
| 38 | + opts = &slog.HandlerOptions{} |
| 39 | + } |
| 40 | + h.opts = *opts |
| 41 | + |
| 42 | + return h |
| 43 | +} |
| 44 | + |
| 45 | +func (h *PrettyHandler) clone() *PrettyHandler { |
| 46 | + return &PrettyHandler{ |
| 47 | + groups: h.groups, |
| 48 | + attrs: h.attrs, |
| 49 | + opts: h.opts, |
| 50 | + mu: h.mu, |
| 51 | + out: h.out, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Enabled implements slog.Handler.Enabled . |
| 56 | +func (h *PrettyHandler) Enabled(_ context.Context, level slog.Level) bool { |
| 57 | + return level >= h.opts.Level.Level() |
| 58 | +} |
| 59 | + |
| 60 | +// Handle implements slog.Handler.Handle . |
| 61 | +func (h *PrettyHandler) Handle(_ context.Context, r slog.Record) error { |
| 62 | + bf := getBuffer() |
| 63 | + bf.Reset() |
| 64 | + |
| 65 | + fmt.Fprint(bf, color.New(color.Faint).Sprint(r.Time.Format(time.RFC3339))) |
| 66 | + fmt.Fprint(bf, " ") |
| 67 | + |
| 68 | + fmt.Fprint(bf, LevelTags[r.Level]) |
| 69 | + fmt.Fprint(bf, " ") |
| 70 | + |
| 71 | + // we need the attributes here, as we can print a longer string if there are no attributes |
| 72 | + stacktrace := "" |
| 73 | + name := "" |
| 74 | + var attrs []slog.Attr |
| 75 | + attrs = append(attrs, h.attrs...) |
| 76 | + r.Attrs(func(a slog.Attr) bool { |
| 77 | + if a.Key == "stack" { |
| 78 | + stacktrace = a.Value.String() |
| 79 | + return true |
| 80 | + } |
| 81 | + if a.Key == "name" { |
| 82 | + name = a.Value.String() |
| 83 | + return true |
| 84 | + } |
| 85 | + attrs = append(attrs, a) |
| 86 | + return true |
| 87 | + }) |
| 88 | + |
| 89 | + if name != "" { |
| 90 | + fmt.Fprint(bf, color.New(color.Faint, color.Bold).Sprint(name)) |
| 91 | + fmt.Fprint(bf, " ") |
| 92 | + } |
| 93 | + |
| 94 | + if stacktrace != "" { |
| 95 | + if r.PC != 0 { |
| 96 | + f, _ := runtime.CallersFrames([]uintptr{r.PC}).Next() |
| 97 | + |
| 98 | + filename := f.File |
| 99 | + lineStr := fmt.Sprintf(":%d", f.Line) |
| 100 | + formatted := fmt.Sprintf("%s ", filename+lineStr) |
| 101 | + fmt.Fprint(bf, formatted) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fmt.Fprint(bf, color.New(color.FgHiWhite).Sprint(r.Message)) |
| 106 | + |
| 107 | + for _, a := range attrs { |
| 108 | + fmt.Fprint(bf, " ") |
| 109 | + for i, g := range h.groups { |
| 110 | + fmt.Fprint(bf, color.New(color.FgWhite).Sprint(g)) |
| 111 | + if i != len(h.groups) { |
| 112 | + fmt.Fprint(bf, color.New(color.FgWhite).Sprint(".")) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + value := color.New(color.FgWhite).Sprint(a.Value.String()) |
| 117 | + if strings.Contains(a.Key, "err") { |
| 118 | + fmt.Fprint(bf, color.New(color.FgRed).Sprintf("%s=", a.Key)+value) |
| 119 | + } else { |
| 120 | + fmt.Fprint(bf, color.New(color.Faint).Sprintf("%s=", a.Key)+value) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if stacktrace != "" { |
| 125 | + fmt.Fprint(bf, "\n") |
| 126 | + fmt.Fprint(bf, stacktrace) |
| 127 | + } |
| 128 | + |
| 129 | + fmt.Fprint(bf, "\n") |
| 130 | + |
| 131 | + h.mu.Lock() |
| 132 | + _, err := io.Copy(h.out, bf) |
| 133 | + h.mu.Unlock() |
| 134 | + |
| 135 | + freeBuffer(bf) |
| 136 | + |
| 137 | + return err |
| 138 | +} |
| 139 | + |
| 140 | +// WithGroup implements slog.Handler.WithGroup . |
| 141 | +func (h *PrettyHandler) WithGroup(name string) slog.Handler { |
| 142 | + h2 := h.clone() |
| 143 | + h2.groups = append(h2.groups, name) |
| 144 | + return h2 |
| 145 | +} |
| 146 | + |
| 147 | +// WithAttrs implements slog.Handler.WithAttrs . |
| 148 | +func (h *PrettyHandler) WithAttrs(attrs []slog.Attr) slog.Handler { |
| 149 | + h2 := h.clone() |
| 150 | + h2.attrs = append(h2.attrs, attrs...) |
| 151 | + return h2 |
| 152 | +} |
0 commit comments