2022-09-11 16:29:12 +00:00
|
|
|
package telegramnotifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
var limiter = rate.NewLimiter(rate.Every(time.Minute), 3)
|
|
|
|
|
|
|
|
type LogHook struct {
|
|
|
|
notifier *Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogHook(notifier *Notifier) *LogHook {
|
|
|
|
return &LogHook{
|
|
|
|
notifier: notifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LogHook) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
logrus.FatalLevel,
|
|
|
|
logrus.PanicLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LogHook) Fire(e *logrus.Entry) error {
|
|
|
|
if !limiter.Allow() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var message = fmt.Sprintf("[%s] %s", e.Level.String(), e.Message)
|
2022-09-19 06:55:58 +00:00
|
|
|
if errData, ok := e.Data[logrus.ErrorKey]; ok {
|
|
|
|
if err, isErr := errData.(error); isErr {
|
|
|
|
message += " Error: " + err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 16:29:12 +00:00
|
|
|
t.notifier.Notify(message)
|
|
|
|
return nil
|
|
|
|
}
|