-
Notifications
You must be signed in to change notification settings - Fork 191
/
log-notifier.go
38 lines (30 loc) · 930 Bytes
/
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
package notifier
import (
"log"
"os"
"path"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type LogNotifier struct {
LogFile string
}
func (logNotifier *LogNotifier) Notify(alerts Messages) bool {
logrus.Println("logging messages...")
logDir := path.Dir(logNotifier.LogFile)
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.LogFile, 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
}