1 Star 0 Fork 0

ryancartoon/sensu-go

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
report.go 1.69 KB
Copy Edit Raw Blame History
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

Search