-
Notifications
You must be signed in to change notification settings - Fork 0
/
plain-handler.go
50 lines (41 loc) · 880 Bytes
/
plain-handler.go
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
package logging
import (
"context"
"io"
"log/slog"
"sync"
"unicode"
)
type plainHandler struct {
mu sync.Mutex
w io.Writer
level slog.Level
}
func newPlainHandler(w io.Writer, level slog.Level) *plainHandler {
return &plainHandler{
mu: sync.Mutex{},
w: w,
level: level,
}
}
func (ph *plainHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= ph.level
}
func (ph *plainHandler) Handle(_ context.Context, r slog.Record) error {
runes := []rune(r.Message)
// upper case the first letter
if len(runes) > 0 {
runes[0] = unicode.ToUpper(runes[0])
}
runes = append(runes, '\n')
ph.mu.Lock()
_, err := ph.w.Write([]byte(string(runes)))
ph.mu.Unlock()
return err
}
func (ph *plainHandler) WithAttrs(_ []slog.Attr) slog.Handler {
return ph
}
func (ph *plainHandler) WithGroup(_ string) slog.Handler {
return ph
}