mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
Merge pull request #937 from c9s/feature/telegram-error-log-hook
notifier: redirect error, panic, fatal error to telegram
This commit is contained in:
commit
4ea723d1d8
|
@ -1012,6 +1012,8 @@ func (environ *Environment) setupTelegram(userConfig *Config, telegramBotToken s
|
|||
var notifier = telegramnotifier.New(bot, opts...)
|
||||
Notification.AddNotifier(notifier)
|
||||
|
||||
log.AddHook(telegramnotifier.NewLogHook(notifier))
|
||||
|
||||
// allocate a store, so that we can save the chatID for the owner
|
||||
var messenger = interact.NewTelegram(bot)
|
||||
|
||||
|
|
39
pkg/notifier/telegramnotifier/logrus_look.go
Normal file
39
pkg/notifier/telegramnotifier/logrus_look.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
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)
|
||||
t.notifier.Notify(message)
|
||||
return nil
|
||||
}
|
|
@ -35,7 +35,7 @@ func UseBroadcast() Option {
|
|||
}
|
||||
}
|
||||
|
||||
// New
|
||||
// New returns a telegram notifier instance
|
||||
func New(bot *telebot.Bot, options ...Option) *Notifier {
|
||||
notifier := &Notifier{
|
||||
bot: bot,
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var limiter = rate.NewLimiter(rate.Every(time.Minute), 45)
|
||||
var limiter = rate.NewLimiter(rate.Every(time.Minute), 3)
|
||||
|
||||
type LogHook struct {
|
||||
Slack *slack.Client
|
||||
|
|
Loading…
Reference in New Issue
Block a user