mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-05 08:42:12 +00:00
Make the DistinctErrorLogger more generic
This commit is contained in:
parent
3b3e771d61
commit
3054a46185
|
@ -181,15 +181,20 @@ func ThemeSet() bool {
|
||||||
return viper.GetString("theme") != ""
|
return viper.GetString("theme") != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// DistinctErrorLogger ignores duplicate log statements.
|
type logPrinter interface {
|
||||||
type DistinctErrorLogger struct {
|
Println(a ...interface{})
|
||||||
sync.RWMutex
|
|
||||||
m map[string]bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf will ERROR log the string returned from fmt.Sprintf given the arguments,
|
// DistinctLogger ignores duplicate log statements.
|
||||||
|
type DistinctLogger struct {
|
||||||
|
sync.RWMutex
|
||||||
|
logger logPrinter
|
||||||
|
m map[string]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf will log the string returned from fmt.Sprintf given the arguments,
|
||||||
// but not if it has been logged before.
|
// but not if it has been logged before.
|
||||||
func (l *DistinctErrorLogger) Printf(format string, v ...interface{}) {
|
func (l *DistinctLogger) Printf(format string, v ...interface{}) {
|
||||||
logStatement := fmt.Sprintf(format, v...)
|
logStatement := fmt.Sprintf(format, v...)
|
||||||
l.RLock()
|
l.RLock()
|
||||||
if l.m[logStatement] {
|
if l.m[logStatement] {
|
||||||
|
@ -200,15 +205,16 @@ func (l *DistinctErrorLogger) Printf(format string, v ...interface{}) {
|
||||||
|
|
||||||
l.Lock()
|
l.Lock()
|
||||||
if !l.m[logStatement] {
|
if !l.m[logStatement] {
|
||||||
jww.ERROR.Print(logStatement)
|
l.logger.Println(logStatement)
|
||||||
l.m[logStatement] = true
|
l.m[logStatement] = true
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
l.Unlock()
|
l.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDistinctErrorLogger creates a new DistinctErrorLogger
|
// NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs
|
||||||
func NewDistinctErrorLogger() *DistinctErrorLogger {
|
func NewDistinctErrorLogger() *DistinctLogger {
|
||||||
return &DistinctErrorLogger{m: make(map[string]bool)}
|
return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid spamming the logs with errors
|
// Avoid spamming the logs with errors
|
||||||
|
|
Loading…
Reference in a new issue