diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-07 17:36:48 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-07 20:11:03 +0300 |
commit | f55d2f43769053b80b419a690554e747dc5dcede (patch) | |
tree | f8c5bdc95df123f2abb04d66b87af4bf9237f086 /tpl/fmt | |
parent | 282f1aa3db9f6420fdd360e46db1ffadd5b083a1 (diff) |
tpl/fmt: Add erroridf template func
Fixes #8613
Diffstat (limited to 'tpl/fmt')
-rw-r--r-- | tpl/fmt/fmt.go | 25 | ||||
-rw-r--r-- | tpl/fmt/init.go | 7 | ||||
-rw-r--r-- | tpl/fmt/init_test.go | 2 |
3 files changed, 25 insertions, 9 deletions
diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go index 713088b57..9c16ca656 100644 --- a/tpl/fmt/fmt.go +++ b/tpl/fmt/fmt.go @@ -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 "" } diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go index 6a2c9a856..f322f5117 100644 --- a/tpl/fmt/init.go +++ b/tpl/fmt/init.go @@ -57,6 +57,13 @@ func init() { }, ) + ns.AddMethodMapping(ctx.Erroridf, + []string{"erroridf"}, + [][2]string{ + {`{{ erroridf "my-err-id" "%s." "failed" }}`, ``}, + }, + ) + ns.AddMethodMapping(ctx.Warnf, []string{"warnf"}, [][2]string{ diff --git a/tpl/fmt/init_test.go b/tpl/fmt/init_test.go index edc1dbb5e..8fa3945b8 100644 --- a/tpl/fmt/init_test.go +++ b/tpl/fmt/init_test.go @@ -30,7 +30,7 @@ func TestInit(t *testing.T) { var ns *internal.TemplateFuncsNamespace for _, nsf := range internal.TemplateFuncsNamespaceRegistry { - ns = nsf(&deps.Deps{Log: loggers.NewErrorLogger()}) + ns = nsf(&deps.Deps{Log: loggers.NewIgnorableLogger(loggers.NewErrorLogger())}) if ns.Name == name { found = true break |