Go 如何检查通过logrus生成的日志,以获得错误和警告数量的计数
我想找到一种方法,通过这种方法,我们可以统计logrus日志中发生的错误和警告的数量 我想用一个结构来记录错误和警告Go 如何检查通过logrus生成的日志,以获得错误和警告数量的计数,go,logging,Go,Logging,我想找到一种方法,通过这种方法,我们可以统计logrus日志中发生的错误和警告的数量 我想用一个结构来记录错误和警告 type ErrorsAndWarnings struct{ Errors int Warnings int } 你可以在logrus中使用钩子。每次调用某个级别的日志时,都会调用钩子 // You can make Errors and Warnings as atomic integers or probably // us
type ErrorsAndWarnings struct{
Errors int
Warnings int
}
你可以在logrus中使用钩子。每次调用某个级别的日志时,都会调用钩子
// You can make Errors and Warnings as atomic integers or probably
// use locks to make it thread safe
type ErrorsAndWarnings struct{
Errors int
Warnings int
}
var (
levels = []logrus.Level{logrus.WarnLevel, logrus.ErrorLevel}
)
func (h *ErrorsAndWarnings) Levels() []logrus.Level {
return levels
}
func (h *ErrorsAndWarnings) Fire(entry *logrus.Entry) error {
if entry.Level == logrus.ErrorLevel{
h.Errors++
}else if entry.Level == logrus.WarnLevel{
h.Warnings++
}
return nil
}
如果有任何方法可以通过logrus生成的日志获得错误和警告的计数。