-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhook.go
208 lines (169 loc) · 4.87 KB
/
hook.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
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
package logrus2telegram
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
"bytes"
"encoding/json"
"errors"
"net/http"
)
const sendMessageURLTemplate = "https://api.telegram.org/bot%s/sendMessage"
// TelegramBotHook is a hook for Logrus logging library to send logs directly to Telegram.
type TelegramBotHook struct {
levels []logrus.Level
notifyOn map[logrus.Level]struct{}
client *http.Client
url string
chatIDs []int64
format func(e *logrus.Entry) (string, error)
requestTimeout time.Duration
}
// config for hook.
type config struct {
levels []logrus.Level
notifyOn map[logrus.Level]struct{}
requestTimeout time.Duration
format func(e *logrus.Entry) (string, error)
client *http.Client
}
// Option configures the hook instance.
type Option func(*config) error
// UseClient sets custom HTTP client for hook.
func UseClient(httpClient *http.Client) Option {
return func(h *config) error {
if httpClient == nil {
return errors.New("HTTP client is not specified")
}
h.client = httpClient
return nil
}
}
// NotifyOn enables notification in messages for specified log levels.
func NotifyOn(levels []logrus.Level) Option {
return func(h *config) error {
if len(levels) < 1 {
return errors.New("at least one level for notification is required")
}
for _, level := range levels {
h.notifyOn[level] = struct{}{}
}
return nil
}
}
// Levels allows to specify levels for the hook.
func Levels(levels []logrus.Level) Option {
return func(h *config) error {
if len(levels) < 1 {
return errors.New("at least one level is required")
}
h.levels = levels
return nil
}
}
// Format specifies the format function for the log entry.
func Format(format func(e *logrus.Entry) (string, error)) Option {
return func(h *config) error {
if format == nil {
return errors.New("the format function is nil")
}
h.format = format
return nil
}
}
// RequestTimeout specifies HTTP request timeout to Telegram API.
func RequestTimeout(requestTimeout time.Duration) Option {
return func(h *config) error {
if requestTimeout < 0 {
return errors.New("the request timeout must be positive")
}
h.requestTimeout = requestTimeout
return nil
}
}
func defaultFormat(entry *logrus.Entry) (string, error) {
m, err := entry.String()
if err != nil {
return "", fmt.Errorf("failed to serialize log entry: %w", err)
}
return m, nil
}
// NewHook creates new hook instance.
func NewHook(token string, chatIDs []int64, options ...Option) (*TelegramBotHook, error) {
if len(chatIDs) < 1 {
return nil, errors.New("at least one chatID is required")
}
c := &config{
levels: []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel, logrus.WarnLevel, logrus.InfoLevel},
format: defaultFormat,
notifyOn: make(map[logrus.Level]struct{}),
requestTimeout: 3 * time.Second,
client: &http.Client{},
}
for _, option := range options {
err := option(c)
if err != nil {
return nil, err
}
}
return &TelegramBotHook{
client: c.client,
url: fmt.Sprintf(sendMessageURLTemplate, token),
chatIDs: chatIDs,
format: c.format,
notifyOn: c.notifyOn,
levels: c.levels,
requestTimeout: c.requestTimeout,
}, nil
}
// message is JSON payload representation sent to Telegram API.
type message struct {
ChatID int64 `json:"chat_id"`
Text string `json:"text"`
DisableNotification bool `json:"disable_notification"`
}
// Fire sends the log entry to Telegram.
func (h *TelegramBotHook) Fire(entry *logrus.Entry) error {
text, err := h.format(entry)
if err != nil {
return fmt.Errorf("failed to format log entry: %w", err)
}
disableNotification := !h.notify(entry.Level)
for _, chatID := range h.chatIDs {
encoded, err := json.Marshal(message{chatID, text, disableNotification})
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), h.requestTimeout)
defer cancel()
request, err := http.NewRequestWithContext(ctx, http.MethodPost, h.url, bytes.NewBuffer(encoded))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
response, err := h.client.Do(request)
if err != nil {
return fmt.Errorf("failed to send HTTP request to Telegram API: %w", err)
} else if response.StatusCode != http.StatusOK {
return fmt.Errorf("response status code is not 200, it is %d", response.StatusCode)
}
if err := response.Body.Close(); err != nil {
return err
}
}
return nil
}
func (h *TelegramBotHook) notify(l logrus.Level) bool {
if len(h.notifyOn) == 0 {
return true
}
if _, notify := h.notifyOn[l]; notify {
return true
}
return false
}
// Levels define on which log levels this hook would trigger.
func (h *TelegramBotHook) Levels() []logrus.Level {
return h.levels
}