summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-06-16 08:17:42 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-06-18 13:03:04 +0200
commit7c9fada778e91976d4ba1cbe942235a9bbeaf5cb (patch)
treea717f6e0a5915777ae6859564acd13385213bbab /tpl
parent0e7944658660b5658b7640dce3cb346d7198d8c9 (diff)
Replace the old log setup, with structured logging etc.
Fixes #11124
Diffstat (limited to 'tpl')
-rw-r--r--tpl/collections/collections.go3
-rw-r--r--tpl/data/data.go5
-rw-r--r--tpl/data/data_test.go11
-rw-r--r--tpl/data/resources_test.go6
-rw-r--r--tpl/fmt/fmt.go51
5 files changed, 50 insertions, 26 deletions
diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go
index 35a87394a..04b777dfb 100644
--- a/tpl/collections/collections.go
+++ b/tpl/collections/collections.go
@@ -30,7 +30,6 @@ import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/deps"
- "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/tpl/compare"
"github.com/spf13/cast"
@@ -393,7 +392,7 @@ func (ns *Namespace) IsSet(c any, key any) (bool, error) {
return av.MapIndex(kv).IsValid(), nil
}
default:
- helpers.DistinctErrorLog.Printf("WARNING: calling IsSet with unsupported type %q (%T) will always return false.\n", av.Kind(), c)
+ ns.deps.Log.Warnf("calling IsSet with unsupported type %q (%T) will always return false.\n", av.Kind(), c)
}
return false, nil
diff --git a/tpl/data/data.go b/tpl/data/data.go
index 251cf1a4f..380c25685 100644
--- a/tpl/data/data.go
+++ b/tpl/data/data.go
@@ -30,7 +30,6 @@ import (
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/common/constants"
- "github.com/gohugoio/hugo/common/loggers"
"github.com/spf13/cast"
@@ -92,7 +91,7 @@ func (ns *Namespace) GetCSV(sep string, args ...any) (d [][]string, err error) {
if security.IsAccessDenied(err) {
return nil, err
}
- ns.deps.Log.(loggers.IgnorableLogger).Errorsf(constants.ErrRemoteGetCSV, "Failed to get CSV resource %q: %s", url, err)
+ ns.deps.Log.Errorsf(constants.ErrRemoteGetCSV, "Failed to get CSV resource %q: %s", url, err)
return nil, nil
}
@@ -128,7 +127,7 @@ func (ns *Namespace) GetJSON(args ...any) (any, error) {
if security.IsAccessDenied(err) {
return nil, err
}
- ns.deps.Log.(loggers.IgnorableLogger).Errorsf(constants.ErrRemoteGetJSON, "Failed to get JSON resource %q: %s", url, err)
+ ns.deps.Log.Errorsf(constants.ErrRemoteGetJSON, "Failed to get JSON resource %q: %s", url, err)
return nil, nil
}
diff --git a/tpl/data/data_test.go b/tpl/data/data_test.go
index f10b88a32..c51dbbd82 100644
--- a/tpl/data/data_test.go
+++ b/tpl/data/data_test.go
@@ -22,6 +22,7 @@ import (
"strings"
"testing"
+ "github.com/bep/logg"
"github.com/gohugoio/hugo/common/maps"
qt "github.com/frankban/quicktest"
@@ -108,13 +109,13 @@ func TestGetCSV(t *testing.T) {
got, err := ns.GetCSV(test.sep, test.url)
if _, ok := test.expect.(bool); ok {
- c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 1)
+ c.Assert(int(ns.deps.Log.LoggCount(logg.LevelError)), qt.Equals, 1)
c.Assert(got, qt.IsNil)
return
}
c.Assert(err, qt.IsNil, msg)
- c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 0)
+ c.Assert(int(ns.deps.Log.LoggCount(logg.LevelError)), qt.Equals, 0)
c.Assert(got, qt.Not(qt.IsNil), msg)
c.Assert(got, qt.DeepEquals, test.expect, msg)
})
@@ -200,11 +201,11 @@ func TestGetJSON(t *testing.T) {
got, _ := ns.GetJSON(test.url)
if _, ok := test.expect.(bool); ok {
- c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 1)
+ c.Assert(int(ns.deps.Log.LoggCount(logg.LevelError)), qt.Equals, 1)
return
}
- c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 0, msg)
+ c.Assert(int(ns.deps.Log.LoggCount(logg.LevelError)), qt.Equals, 0, msg)
c.Assert(got, qt.Not(qt.IsNil), msg)
c.Assert(got, qt.DeepEquals, test.expect)
@@ -283,7 +284,7 @@ func TestHeaders(t *testing.T) {
err := fn("http://example.org/api", "?foo", test.headers)
c.Assert(err, qt.IsNil)
- c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 0)
+ c.Assert(int(ns.deps.Log.LoggCount(logg.LevelError)), qt.Equals, 0)
test.assert(c, headers.String())
}
diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go
index ad4ab20f4..d452a2a43 100644
--- a/tpl/data/resources_test.go
+++ b/tpl/data/resources_test.go
@@ -15,6 +15,9 @@ package data
import (
"bytes"
+
+ "github.com/gohugoio/hugo/common/loggers"
+
"net/http"
"net/http/httptest"
"net/url"
@@ -29,7 +32,6 @@ import (
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/cache/filecache"
- "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
@@ -182,7 +184,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
func newDeps(cfg config.Provider) *deps.Deps {
conf := testconfig.GetTestConfig(nil, cfg)
- logger := loggers.NewIgnorableLogger(loggers.NewErrorLogger(), nil)
+ logger := loggers.NewDefault()
fs := hugofs.NewFrom(afero.NewMemMapFs(), conf.BaseConfig())
d := &deps.Deps{
diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go
index 0667bcedd..c0d75e425 100644
--- a/tpl/fmt/fmt.go
+++ b/tpl/fmt/fmt.go
@@ -16,27 +16,22 @@ package fmt
import (
_fmt "fmt"
+ "sort"
+ "github.com/bep/logg"
"github.com/gohugoio/hugo/common/loggers"
-
"github.com/gohugoio/hugo/deps"
- "github.com/gohugoio/hugo/helpers"
+ "github.com/spf13/cast"
)
// New returns a new instance of the fmt-namespaced template functions.
func New(d *deps.Deps) *Namespace {
- ignorableLogger, ok := d.Log.(loggers.IgnorableLogger)
- if !ok {
- ignorableLogger = loggers.NewIgnorableLogger(d.Log, nil)
- }
-
- distinctLogger := helpers.NewDistinctLogger(d.Log)
ns := &Namespace{
- distinctLogger: ignorableLogger.Apply(distinctLogger),
+ logger: d.Log,
}
d.BuildStartListeners.Add(func() {
- ns.distinctLogger.Reset()
+ ns.logger.Reset()
})
return ns
@@ -44,7 +39,7 @@ func New(d *deps.Deps) *Namespace {
// Namespace provides template functions for the "fmt" namespace.
type Namespace struct {
- distinctLogger loggers.IgnorableLogger
+ logger loggers.Logger
}
// Print returns a string representation of args.
@@ -65,7 +60,7 @@ func (ns *Namespace) Println(args ...any) string {
// Errorf formats args according to a format specifier and logs an ERROR.
// It returns an empty string.
func (ns *Namespace) Errorf(format string, args ...any) string {
- ns.distinctLogger.Errorf(format, args...)
+ ns.logger.Errorf(format, args...)
return ""
}
@@ -73,13 +68,41 @@ func (ns *Namespace) Errorf(format string, args ...any) string {
// 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, args ...any) string {
- ns.distinctLogger.Errorsf(id, format, args...)
+ ns.logger.Errorsf(id, format, args...)
return ""
}
// Warnf formats args according to a format specifier and logs a WARNING.
// It returns an empty string.
func (ns *Namespace) Warnf(format string, args ...any) string {
- ns.distinctLogger.Warnf(format, args...)
+ ns.logger.Warnf(format, args...)
+ return ""
+}
+
+// Warnmf is epxermimental and subject to change at any time.
+func (ns *Namespace) Warnmf(m any, format string, args ...any) string {
+ return ns.logmf(ns.logger.Warn(), m, format, args...)
+}
+
+// Errormf is epxermimental and subject to change at any time.
+func (ns *Namespace) Errormf(m any, format string, args ...any) string {
+ return ns.logmf(ns.logger.Error(), m, format, args...)
+}
+
+func (ns *Namespace) logmf(l logg.LevelLogger, m any, format string, args ...any) string {
+ mm := cast.ToStringMap(m)
+ fields := make(logg.Fields, len(mm))
+ i := 0
+ for k, v := range mm {
+ fields[i] = logg.Field{Name: k, Value: v}
+ i++
+ }
+ // Sort the fields to make the output deterministic.
+ sort.Slice(fields, func(i, j int) bool {
+ return fields[i].Name < fields[j].Name
+ })
+
+ l.WithFields(fields).Logf(format, args...)
+
return ""
}