mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-02 22:52:51 +02:00
@@ -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 ""
|
||||
}
|
||||
|
@@ -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{
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user