tpl/fmt: Add erroridf template func

Fixes #8613
This commit is contained in:
Bjørn Erik Pedersen
2021-06-07 16:36:48 +02:00
parent 282f1aa3db
commit f55d2f4376
19 changed files with 170 additions and 71 deletions

View File

@@ -17,20 +17,22 @@ package fmt
import (
_fmt "fmt"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
)
// New returns a new instance of the fmt-namespaced template functions.
func New(d *deps.Deps) *Namespace {
ignorableLogger := d.Log.(loggers.IgnorableLogger)
distinctLogger := helpers.NewDistinctLogger(d.Log)
ns := &Namespace{
errorLogger: helpers.NewDistinctLogger(d.Log.Error()),
warnLogger: helpers.NewDistinctLogger(d.Log.Warn()),
distinctLogger: ignorableLogger.Apply(distinctLogger),
}
d.BuildStartListeners.Add(func() {
ns.errorLogger.Reset()
ns.warnLogger.Reset()
ns.distinctLogger.Reset()
})
return ns
@@ -38,8 +40,7 @@ func New(d *deps.Deps) *Namespace {
// Namespace provides template functions for the "fmt" namespace.
type Namespace struct {
errorLogger *helpers.DistinctLogger
warnLogger *helpers.DistinctLogger
distinctLogger loggers.IgnorableLogger
}
// Print returns string representation of the passed arguments.
@@ -60,13 +61,21 @@ func (ns *Namespace) Println(a ...interface{}) string {
// Errorf formats according to a format specifier and logs an ERROR.
// It returns an empty string.
func (ns *Namespace) Errorf(format string, a ...interface{}) string {
ns.errorLogger.Printf(format, a...)
ns.distinctLogger.Errorf(format, a...)
return ""
}
// Erroridf formats according to a format specifier and logs an ERROR and
// an information text that the error with the given ID can be suppressed in config.
// It returns an empty string.
func (ns *Namespace) Erroridf(id, format string, a ...interface{}) string {
ns.distinctLogger.Errorsf(id, format, a...)
return ""
}
// Warnf formats according to a format specifier and logs a WARNING.
// It returns an empty string.
func (ns *Namespace) Warnf(format string, a ...interface{}) string {
ns.warnLogger.Printf(format, a...)
ns.distinctLogger.Warnf(format, a...)
return ""
}