-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle.go
112 lines (88 loc) · 2.23 KB
/
handle.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
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
package slogtelegram
import (
"context"
"fmt"
"log/slog"
"strings"
)
const errPrefix = "slogtelegram: "
var _ slog.Handler = (*Handler)(nil)
type HandlerOptions struct {
// Level reports the minimum record level that will be logged.
// The handler discards records with lower levels.
// If Level is nil, the handler assumes LevelInfo.
// The handler calls Level.Level for each record processed;
// to adjust the minimum level dynamically, use a LevelVar.
Level slog.Leveler
// Formatter is a config to format the log record before sending.
Formatter FormatterOptions
// Sender is a config to send the formatted log record to a telegram chat.
Sender SenderOptions
}
type Handler struct {
formatter Formatter
sender Sender
minLevel slog.Leveler
attrs []slog.Attr
groups []string
}
func DefaultHandler(token string, chatID int64) *Handler {
return NewHandler(HandlerOptions{
Sender: SenderOptions{
Token: token,
ChatID: chatID,
},
})
}
func NewHandler(opts HandlerOptions) *Handler {
if opts.Level == nil {
opts.Level = slog.LevelInfo
}
return &Handler{
formatter: NewFormatter(opts.Formatter),
sender: NewSender(opts.Sender),
minLevel: opts.Level,
}
}
func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.minLevel.Level()
}
func (h *Handler) Handle(_ context.Context, record slog.Record) error {
record.AddAttrs(h.attrs...)
if len(h.groups) > 0 {
record.AddAttrs(slog.String("groups", strings.Join(h.groups, ", ")))
}
text, err := h.formatter.Format(record)
if err != nil {
return fmt.Errorf("%s%w", errPrefix, err)
}
if err := h.sender.Send(text); err != nil {
return fmt.Errorf("%s%w", errPrefix, err)
}
return nil
}
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
c := h.clone()
c.attrs = append(c.attrs, attrs...)
return c
}
func (h *Handler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
c := h.clone()
c.groups = append(c.groups, name)
return c
}
func (h *Handler) Close() error {
return h.sender.Close()
}
func (h *Handler) clone() *Handler {
c := *h
c.attrs = append([]slog.Attr(nil), h.attrs...)
c.groups = append([]string(nil), h.groups...)
return &c
}