2017-06-16 10:33:35 +02:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
2017-10-25 00:36:16 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2017-08-11 17:45:42 +02:00
|
|
|
"github.com/genofire/logmania/bot"
|
2017-06-16 10:33:35 +02:00
|
|
|
"github.com/genofire/logmania/lib"
|
|
|
|
"github.com/genofire/logmania/notify"
|
2017-08-11 17:45:42 +02:00
|
|
|
configNotify "github.com/genofire/logmania/notify/config"
|
2017-06-16 10:33:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
|
|
|
notify.Notifier
|
2017-08-09 08:45:45 +02:00
|
|
|
list []notify.Notifier
|
|
|
|
channelNotify chan *log.Entry
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 17:45:42 +02:00
|
|
|
func Init(config *lib.NotifyConfig, state *configNotify.NotifyState, bot *bot.Bot) notify.Notifier {
|
2017-06-16 10:33:35 +02:00
|
|
|
var list []notify.Notifier
|
|
|
|
for _, init := range notify.NotifyRegister {
|
2017-08-11 17:45:42 +02:00
|
|
|
notify := init(config, state, bot)
|
2017-06-16 10:33:35 +02:00
|
|
|
|
|
|
|
if notify == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
list = append(list, notify)
|
|
|
|
}
|
2017-08-09 08:45:45 +02:00
|
|
|
|
|
|
|
n := &Notifier{
|
|
|
|
list: list,
|
|
|
|
channelNotify: make(chan *log.Entry),
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
2017-08-09 08:45:45 +02:00
|
|
|
go n.sender()
|
|
|
|
return n
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 08:45:45 +02:00
|
|
|
func (n *Notifier) sender() {
|
|
|
|
for c := range n.channelNotify {
|
|
|
|
for _, item := range n.list {
|
2017-10-25 00:36:16 +02:00
|
|
|
item.Fire(c)
|
2017-08-09 08:45:45 +02:00
|
|
|
}
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 00:36:16 +02:00
|
|
|
func (n *Notifier) Fire(e *log.Entry) error {
|
2017-08-09 08:45:45 +02:00
|
|
|
n.channelNotify <- e
|
2017-10-25 00:36:16 +02:00
|
|
|
return nil
|
2017-08-09 08:45:45 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 10:33:35 +02:00
|
|
|
func (n *Notifier) Close() {
|
|
|
|
for _, item := range n.list {
|
|
|
|
item.Close()
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 00:36:16 +02:00
|
|
|
|
|
|
|
func (n *Notifier) Levels() []log.Level {
|
|
|
|
return []log.Level{
|
|
|
|
log.DebugLevel,
|
|
|
|
log.InfoLevel,
|
|
|
|
log.WarnLevel,
|
|
|
|
log.ErrorLevel,
|
|
|
|
log.FatalLevel,
|
|
|
|
log.PanicLevel,
|
|
|
|
}
|
|
|
|
}
|