forked from AcalephStorage/consul-alerts
/
log-notifier.go
50 lines (40 loc) · 1.2 KB
/
log-notifier.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
package notifier
import (
"log"
"os"
"path"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type LogNotifier struct {
Enabled bool
Path string `json:"path"`
}
// NotifierName provides name for notifier selection
func (logNotifier *LogNotifier) NotifierName() string {
return "log"
}
func (logNotifier *LogNotifier) Copy() Notifier {
notifier := *logNotifier
return ¬ifier
}
//Notify sends messages to the endpoint notifier
func (logNotifier *LogNotifier) Notify(alerts Messages) bool {
logrus.Println("logging messages...")
logDir := path.Dir(logNotifier.Path)
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
logrus.Printf("unable to create directory for logfile: %v\n", err)
return false
}
file, err := os.OpenFile(logNotifier.Path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
logrus.Printf("unable to write to logfile: %v\n", err)
return false
}
logger := log.New(file, "[consul-notifier] ", log.LstdFlags)
for _, alert := range alerts {
logger.Printf("Node=%s, Service=%s, Check=%s, Status=%s\n", alert.Node, alert.Service, alert.Check, alert.Status)
}
logrus.Println("Notifications logged.")
return true
}