mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-15 20:44:01 +02:00
@@ -24,13 +24,11 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hugo"
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
@@ -254,143 +252,6 @@ func compareStringSlices(a, b []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DistinctLogger ignores duplicate log statements.
|
||||
type DistinctLogger struct {
|
||||
loggers.Logger
|
||||
sync.RWMutex
|
||||
m map[string]bool
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Reset() {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
l.m = make(map[string]bool)
|
||||
}
|
||||
|
||||
// Println will log the string returned from fmt.Sprintln given the arguments,
|
||||
// but not if it has been logged before.
|
||||
func (l *DistinctLogger) Println(v ...any) {
|
||||
// fmt.Sprint doesn't add space between string arguments
|
||||
logStatement := strings.TrimSpace(fmt.Sprintln(v...))
|
||||
l.printIfNotPrinted("println", logStatement, func() {
|
||||
l.Logger.Println(logStatement)
|
||||
})
|
||||
}
|
||||
|
||||
// Printf will log the string returned from fmt.Sprintf given the arguments,
|
||||
// but not if it has been logged before.
|
||||
func (l *DistinctLogger) Printf(format string, v ...any) {
|
||||
logStatement := fmt.Sprintf(format, v...)
|
||||
l.printIfNotPrinted("printf", logStatement, func() {
|
||||
l.Logger.Printf(format, v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Debugf(format string, v ...any) {
|
||||
logStatement := fmt.Sprintf(format, v...)
|
||||
l.printIfNotPrinted("debugf", logStatement, func() {
|
||||
l.Logger.Debugf(format, v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Debugln(v ...any) {
|
||||
logStatement := fmt.Sprint(v...)
|
||||
l.printIfNotPrinted("debugln", logStatement, func() {
|
||||
l.Logger.Debugln(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Infof(format string, v ...any) {
|
||||
logStatement := fmt.Sprintf(format, v...)
|
||||
l.printIfNotPrinted("info", logStatement, func() {
|
||||
l.Logger.Infof(format, v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Infoln(v ...any) {
|
||||
logStatement := fmt.Sprint(v...)
|
||||
l.printIfNotPrinted("infoln", logStatement, func() {
|
||||
l.Logger.Infoln(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Warnf(format string, v ...any) {
|
||||
logStatement := fmt.Sprintf(format, v...)
|
||||
l.printIfNotPrinted("warnf", logStatement, func() {
|
||||
l.Logger.Warnf(format, v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Warnln(v ...any) {
|
||||
logStatement := fmt.Sprint(v...)
|
||||
l.printIfNotPrinted("warnln", logStatement, func() {
|
||||
l.Logger.Warnln(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Errorf(format string, v ...any) {
|
||||
logStatement := fmt.Sprint(v...)
|
||||
l.printIfNotPrinted("errorf", logStatement, func() {
|
||||
l.Logger.Errorf(format, v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) Errorln(v ...any) {
|
||||
logStatement := fmt.Sprint(v...)
|
||||
l.printIfNotPrinted("errorln", logStatement, func() {
|
||||
l.Logger.Errorln(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) hasPrinted(key string) bool {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
_, found := l.m[key]
|
||||
return found
|
||||
}
|
||||
|
||||
func (l *DistinctLogger) printIfNotPrinted(level, logStatement string, print func()) {
|
||||
key := level + logStatement
|
||||
if l.hasPrinted(key) {
|
||||
return
|
||||
}
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
l.m[key] = true // Placing this after print() can cause duplicate warning entries to be logged when --panicOnWarning is true.
|
||||
print()
|
||||
|
||||
}
|
||||
|
||||
// NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs
|
||||
func NewDistinctErrorLogger() loggers.Logger {
|
||||
return &DistinctLogger{m: make(map[string]bool), Logger: loggers.NewErrorLogger()}
|
||||
}
|
||||
|
||||
// NewDistinctLogger creates a new DistinctLogger that logs to the provided logger.
|
||||
func NewDistinctLogger(logger loggers.Logger) loggers.Logger {
|
||||
return &DistinctLogger{m: make(map[string]bool), Logger: logger}
|
||||
}
|
||||
|
||||
// NewDistinctWarnLogger creates a new DistinctLogger that logs WARNs
|
||||
func NewDistinctWarnLogger() loggers.Logger {
|
||||
return &DistinctLogger{m: make(map[string]bool), Logger: loggers.NewWarningLogger()}
|
||||
}
|
||||
|
||||
var (
|
||||
// DistinctErrorLog can be used to avoid spamming the logs with errors.
|
||||
DistinctErrorLog = NewDistinctErrorLogger()
|
||||
|
||||
// DistinctWarnLog can be used to avoid spamming the logs with warnings.
|
||||
DistinctWarnLog = NewDistinctWarnLogger()
|
||||
)
|
||||
|
||||
// InitLoggers resets the global distinct loggers.
|
||||
func InitLoggers() {
|
||||
DistinctErrorLog.Reset()
|
||||
DistinctWarnLog.Reset()
|
||||
}
|
||||
|
||||
// Deprecated informs about a deprecation, but only once for a given set of arguments' values.
|
||||
// If the err flag is enabled, it logs as an ERROR (will exit with -1) and the text will
|
||||
// point at the next Hugo release.
|
||||
@@ -398,13 +259,9 @@ func InitLoggers() {
|
||||
// plenty of time to fix their templates.
|
||||
func Deprecated(item, alternative string, err bool) {
|
||||
if err {
|
||||
DistinctErrorLog.Errorf("%s is deprecated and will be removed in Hugo %s. %s", item, hugo.CurrentVersion.Next().ReleaseVersion(), alternative)
|
||||
loggers.Log().Errorf("%s is deprecated and will be removed in Hugo %s. %s", item, hugo.CurrentVersion.Next().ReleaseVersion(), alternative)
|
||||
} else {
|
||||
var warnPanicMessage string
|
||||
if !loggers.PanicOnWarning.Load() {
|
||||
warnPanicMessage = "\n\nRe-run Hugo with the flag --panicOnWarning to get a better error message."
|
||||
}
|
||||
DistinctWarnLog.Warnf("%s is deprecated and will be removed in a future release. %s%s", item, alternative, warnPanicMessage)
|
||||
loggers.Log().Warnf("%s is deprecated and will be removed in a future release. %s%s", item, alternative)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user