-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger.go
66 lines (56 loc) · 1.23 KB
/
logger.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"sync"
"time"
)
var (
loggerInstance logger
)
type logRecord struct {
time time.Time
isNormal bool
statusCode int
isTimeout bool
err error
}
type logger struct {
sync.Mutex
records []logRecord
}
func (l *logger) string() string {
var msg string
for _, record := range l.records {
msg += fmt.Sprintf("* %s\tnormal: %v\tstatus code: %d\ttimeout: %v\terror: %v\n",
record.time.Format("2006-01-02 15:04:05"), record.isNormal, record.statusCode, record.isTimeout, record.err)
}
return msg
}
func logPush(record logRecord) {
loggerInstance.Lock()
defer loggerInstance.Unlock()
if len(loggerInstance.records) >= 10 {
loggerInstance.records = loggerInstance.records[1:]
}
loggerInstance.records = append(loggerInstance.records, record)
logCheck()
}
func logCheck() {
var unNormal int
for _, r := range loggerInstance.records {
if !r.isNormal {
unNormal++
}
}
if unNormal >= 5 {
debug("* Site status: unhealthy")
go sendMail(loggerInstance.string())
} else if float64(unNormal)/float64(len(loggerInstance.records)) > 0.5 {
debug("* Site status: unhealthy")
} else {
debug("* Site status: healthy")
}
}
func init() {
loggerInstance.records = make([]logRecord, 0, 10)
}