-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
91 lines (74 loc) · 1.69 KB
/
report.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package report
import (
"fmt"
"io"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/sensu/sensu-go/cli/elements/globals"
)
// Report reports debug, warning, & errors
type Report struct {
Out io.Writer
LogLevel logrus.Level
entries []Entry
}
// New returns new report w/ log level preconfigured
func New() Report {
return Report{LogLevel: logrus.InfoLevel}
}
// Flush pops entries from list and writes them
func (r *Report) Flush() error {
entries := make([]Entry, len(r.entries))
copy(entries, r.entries)
r.entries = []Entry{}
for _, entry := range entries {
if entry.Level > r.LogLevel {
continue
}
level := strings.ToUpper(entry.Level.String())
if entry.Level == logrus.WarnLevel {
level = globals.WarningStyle(level)
} else if entry.Level <= logrus.ErrorLevel {
level = globals.ErrorTextStyle(level)
} else if entry.Level == logrus.InfoLevel {
level = globals.PrimaryTextStyle(level)
}
fmt.Fprintf(
r.Out,
"%s\r\t%s\n",
level,
entry.Message,
)
}
return nil
}
// HasWarnings returns true if there are any warnings entries in the report
func (r *Report) HasWarnings() bool {
for _, entry := range r.entries {
if entry.Level == logrus.WarnLevel {
return true
}
}
return false
}
// HasErrors returns true if there are any error entries in the report
func (r *Report) HasErrors() bool {
for _, entry := range r.entries {
if entry.Level <= logrus.ErrorLevel {
return true
}
}
return false
}
// AddEntry adds given entry to report
func (r *Report) AddEntry(e Entry) {
r.entries = append(r.entries, e)
}
// Entry ...
type Entry struct {
Level logrus.Level
Message string
Context map[string]interface{}
Time time.Time
}