From 7c9fada778e91976d4ba1cbe942235a9bbeaf5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 16 Jun 2023 08:17:42 +0200 Subject: Replace the old log setup, with structured logging etc. Fixes #11124 --- hugolib/alias.go | 1 - hugolib/alias_test.go | 5 ++--- hugolib/config_test.go | 3 ++- hugolib/filesystems/basefs.go | 4 ++-- hugolib/hugo_modules_test.go | 10 +++++----- hugolib/hugo_sites.go | 6 ++---- hugolib/hugo_sites_build.go | 34 ++++++++++++++++++---------------- hugolib/hugo_sites_build_test.go | 2 +- hugolib/integrationtest_builder.go | 12 +++++------- hugolib/mount_filters_test.go | 3 +-- hugolib/page__new.go | 2 +- hugolib/page_test.go | 8 +++----- hugolib/pagebundler_test.go | 10 ++++++---- hugolib/pages_capture.go | 2 +- hugolib/pages_capture_test.go | 2 +- hugolib/renderstring_test.go | 5 +++-- hugolib/resource_chain_test.go | 2 +- hugolib/site.go | 23 ++++++++++------------- hugolib/site_new.go | 37 +++++++++++++++++++++++++++++++------ hugolib/site_render.go | 2 +- hugolib/testhelpers_test.go | 4 ++-- 21 files changed, 98 insertions(+), 79 deletions(-) (limited to 'hugolib') diff --git a/hugolib/alias.go b/hugolib/alias.go index d10f140bd..5165edb04 100644 --- a/hugolib/alias.go +++ b/hugolib/alias.go @@ -25,7 +25,6 @@ import ( "strings" "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/publisher" "github.com/gohugoio/hugo/resources/page" diff --git a/hugolib/alias_test.go b/hugolib/alias_test.go index 124c9f4ca..e03107ada 100644 --- a/hugolib/alias_test.go +++ b/hugolib/alias_test.go @@ -18,9 +18,8 @@ import ( "runtime" "testing" - "github.com/gohugoio/hugo/common/loggers" - qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/common/loggers" ) const pageWithAlias = `--- @@ -117,7 +116,7 @@ func TestAliasTemplate(t *testing.T) { } func TestTargetPathHTMLRedirectAlias(t *testing.T) { - h := newAliasHandler(nil, loggers.NewErrorLogger(), false) + h := newAliasHandler(nil, loggers.NewDefault(), false) errIsNilForThisOS := runtime.GOOS != "windows" diff --git a/hugolib/config_test.go b/hugolib/config_test.go index 5a3087207..02d25fa9b 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -20,6 +20,7 @@ import ( "strings" "testing" + "github.com/bep/logg" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/config/allconfig" @@ -933,7 +934,7 @@ LanguageCode: {{ eq site.LanguageCode site.Language.LanguageCode }}|{{ site.Lang ).Build() { - b.Assert(b.H.Log.LogCounters().WarnCounter.Count(), qt.Equals, uint64(2)) + b.Assert(b.H.Log.LoggCount(logg.LevelWarn), qt.Equals, 1) } b.AssertFileContent("public/index.html", ` AllPages: 4| diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go index ed0d36de1..d5c94799d 100644 --- a/hugolib/filesystems/basefs.go +++ b/hugolib/filesystems/basefs.go @@ -29,9 +29,9 @@ import ( "github.com/gohugoio/hugo/hugofs/glob" "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/types" - "github.com/gohugoio/hugo/common/loggers" "github.com/rogpeppe/go-internal/lockedfile" "github.com/gohugoio/hugo/hugofs/files" @@ -471,7 +471,7 @@ var counter int func NewBase(p *paths.Paths, logger loggers.Logger, options ...func(*BaseFs) error) (*BaseFs, error) { fs := p.Fs if logger == nil { - logger = loggers.NewWarningLogger() + logger = loggers.NewDefault() } publishFs := hugofs.NewBaseFileDecorator(fs.PublishDir) diff --git a/hugolib/hugo_modules_test.go b/hugolib/hugo_modules_test.go index ee4ef798a..3353f508e 100644 --- a/hugolib/hugo_modules_test.go +++ b/hugolib/hugo_modules_test.go @@ -22,16 +22,16 @@ import ( "testing" "time" + "github.com/bep/logg" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/modules/npm" - "github.com/gohugoio/hugo/common/loggers" - "github.com/spf13/afero" "github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/common/hugo" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugofs" @@ -646,14 +646,14 @@ min_version = 0.55.0 `) - logger := loggers.NewWarningLogger() + logger := loggers.NewDefault() b.WithLogger(logger) b.Build(BuildCfg{}) c := qt.New(t) - c.Assert(logger.LogCounters().WarnCounter.Count(), qt.Equals, uint64(3)) + c.Assert(logger.LoggCount(logg.LevelWarn), qt.Equals, 3) } func TestModulesSymlinks(t *testing.T) { @@ -727,7 +727,7 @@ weight = 2 ` b := newTestSitesBuilder(t).WithNothingAdded().WithWorkingDir(workingDir) - b.WithLogger(loggers.NewErrorLogger()) + b.WithLogger(loggers.NewDefault()) b.Fs = fs b.WithConfigFile("toml", config) diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 290eebe82..6ffef6fa8 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -23,6 +23,7 @@ import ( "sync" "sync/atomic" + "github.com/bep/logg" "github.com/gohugoio/hugo/config/allconfig" "github.com/gohugoio/hugo/hugofs/glob" @@ -42,7 +43,6 @@ import ( "github.com/gohugoio/hugo/source" "github.com/gohugoio/hugo/common/herrors" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/lazy" @@ -265,7 +265,7 @@ func (h *HugoSites) NumLogErrors() int { if h == nil { return 0 } - return int(h.Log.LogCounters().ErrorCounter.Count()) + return h.Log.LoggCount(logg.LevelError) } func (h *HugoSites) PrintProcessingStats(w io.Writer) { @@ -352,10 +352,8 @@ func (h *HugoSites) reset(config *BuildCfg) { // resetLogs resets the log counters etc. Used to do a new build on the same sites. func (h *HugoSites) resetLogs() { h.Log.Reset() - loggers.GlobalErrorCounter.Reset() for _, s := range h.Sites { s.Deps.Log.Reset() - s.Deps.LogDistinct.Reset() } } diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index c801ae3df..d90d9ce7b 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -22,6 +22,7 @@ import ( "strings" "time" + "github.com/bep/logg" "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/publisher" @@ -65,6 +66,8 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { defer unlock() } + infol := h.Log.InfoCommand("build") + errCollector := h.StartErrorCollector() errs := make(chan error) @@ -120,11 +123,11 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { return nil } - if err := h.process(conf, init, events...); err != nil { + if err := h.process(infol, conf, init, events...); err != nil { return fmt.Errorf("process: %w", err) } - if err := h.assemble(conf); err != nil { + if err := h.assemble(infol, conf); err != nil { return fmt.Errorf("assemble: %w", err) } @@ -137,10 +140,10 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { } if prepareErr == nil { - if err := h.render(conf); err != nil { + if err := h.render(infol, conf); err != nil { h.SendError(fmt.Errorf("render: %w", err)) } - if err := h.postProcess(); err != nil { + if err := h.postProcess(infol); err != nil { h.SendError(fmt.Errorf("postProcess: %w", err)) } } @@ -164,7 +167,7 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { return err } - errorCount := h.Log.LogCounters().ErrorCounter.Count() + errorCount := h.Log.LoggCount(logg.LevelError) if errorCount > 0 { return fmt.Errorf("logged %d error(s)", errorCount) } @@ -195,13 +198,12 @@ func (h *HugoSites) initRebuild(config *BuildCfg) error { h.reset(config) h.resetLogs() - helpers.InitLoggers() return nil } -func (h *HugoSites) process(config *BuildCfg, init func(config *BuildCfg) error, events ...fsnotify.Event) error { - defer h.timeTrack(time.Now(), "process") +func (h *HugoSites) process(l logg.LevelLogger, config *BuildCfg, init func(config *BuildCfg) error, events ...fsnotify.Event) error { + defer h.timeTrack(l, time.Now(), "process") // We should probably refactor the Site and pull up most of the logic from there to here, // but that seems like a daunting task. @@ -218,8 +220,8 @@ func (h *HugoSites) process(config *BuildCfg, init func(config *BuildCfg) error, return firstSite.process(*config) } -func (h *HugoSites) assemble(bcfg *BuildCfg) error { - defer h.timeTrack(time.Now(), "assemble") +func (h *HugoSites) assemble(l logg.LevelLogger, bcfg *BuildCfg) error { + defer h.timeTrack(l, time.Now(), "assemble") if !bcfg.whatChanged.source { return nil @@ -236,13 +238,13 @@ func (h *HugoSites) assemble(bcfg *BuildCfg) error { return nil } -func (h *HugoSites) timeTrack(start time.Time, name string) { +func (h *HugoSites) timeTrack(l logg.LevelLogger, start time.Time, name string) { elapsed := time.Since(start) - h.Log.Infof("%s in %v ms\n", name, int(1000*elapsed.Seconds())) + l.WithField("step", name).WithField("duration", elapsed).Logf("running") } -func (h *HugoSites) render(config *BuildCfg) error { - defer h.timeTrack(time.Now(), "render") +func (h *HugoSites) render(l logg.LevelLogger, config *BuildCfg) error { + defer h.timeTrack(l, time.Now(), "render") if _, err := h.init.layouts.Do(context.Background()); err != nil { return err } @@ -312,8 +314,8 @@ func (h *HugoSites) render(config *BuildCfg) error { return nil } -func (h *HugoSites) postProcess() error { - defer h.timeTrack(time.Now(), "postProcess") +func (h *HugoSites) postProcess(l logg.LevelLogger) error { + defer h.timeTrack(l, time.Now(), "postProcess") // Make sure to write any build stats to disk first so it's available // to the post processors. diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index b2798c863..3c82a60b6 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -1394,7 +1394,7 @@ other = %q } func TestRebuildOnAssetChange(t *testing.T) { - b := newTestSitesBuilder(t).Running().WithLogger(loggers.NewInfoLogger()) + b := newTestSitesBuilder(t).Running().WithLogger(loggers.NewDefault()) b.WithTemplatesAdded("index.html", ` {{ (resources.Get "data.json").Content }} `) diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index 0daa766ac..cb34cb28b 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -13,7 +13,7 @@ import ( "sync" "testing" - jww "github.com/spf13/jwalterweatherman" + "github.com/bep/logg" qt "github.com/frankban/quicktest" "github.com/fsnotify/fsnotify" @@ -292,11 +292,9 @@ func (s *IntegrationTestBuilder) initBuilder() error { } if s.Cfg.LogLevel == 0 { - s.Cfg.LogLevel = jww.LevelWarn + s.Cfg.LogLevel = logg.LevelWarn } - logger := loggers.NewBasicLoggerForWriter(s.Cfg.LogLevel, &s.logBuff) - isBinaryRe := regexp.MustCompile(`^(.*)(\.png|\.jpg)$`) const dataSourceFilenamePrefix = "sourcefilename:" @@ -350,7 +348,7 @@ func (s *IntegrationTestBuilder) initBuilder() error { Flags: flags, ConfigDir: configDir, Fs: afs, - Logger: logger, + Logger: loggers.NewDefault(), Environ: s.Cfg.Environ, }, ) @@ -364,7 +362,7 @@ func (s *IntegrationTestBuilder) initBuilder() error { s.Assert(err, qt.IsNil) - depsCfg := deps.DepsCfg{Configs: res, Fs: fs, Logger: logger} + depsCfg := deps.DepsCfg{Configs: res, Fs: fs, LogLevel: s.Cfg.LogLevel, LogOut: &s.logBuff} sites, err := NewHugoSites(depsCfg) if err != nil { initErr = err @@ -528,7 +526,7 @@ type IntegrationTestConfig struct { // Will print the log buffer after the build Verbose bool - LogLevel jww.Threshold + LogLevel logg.Level // Whether it needs the real file system (e.g. for js.Build tests). NeedsOsFS bool diff --git a/hugolib/mount_filters_test.go b/hugolib/mount_filters_test.go index 688cf2558..4f6a448d2 100644 --- a/hugolib/mount_filters_test.go +++ b/hugolib/mount_filters_test.go @@ -20,7 +20,6 @@ import ( "testing" "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/htesting" @@ -39,7 +38,7 @@ func TestMountFilters(t *testing.T) { for _, component := range files.ComponentFolders { b.Assert(os.MkdirAll(filepath.Join(workingDir, component), 0777), qt.IsNil) } - b.WithWorkingDir(workingDir).WithLogger(loggers.NewInfoLogger()) + b.WithWorkingDir(workingDir).WithLogger(loggers.NewDefault()) b.WithConfigFile("toml", fmt.Sprintf(` workingDir = %q diff --git a/hugolib/page__new.go b/hugolib/page__new.go index e9a8b1a50..14db28c3d 100644 --- a/hugolib/page__new.go +++ b/hugolib/page__new.go @@ -99,7 +99,7 @@ func newPageFromMeta( meta map[string]any, metaProvider *pageMeta) (*pageState, error) { if metaProvider.f == nil { - metaProvider.f = page.NewZeroFile(metaProvider.s.LogDistinct) + metaProvider.f = page.NewZeroFile(metaProvider.s.Log) } ps, err := newPageBase(metaProvider) diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 79b6401dc..6b817551e 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -17,7 +17,6 @@ import ( "context" "fmt" "html/template" - "os" "path/filepath" "strings" "testing" @@ -36,7 +35,6 @@ import ( "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/resource" - "github.com/spf13/jwalterweatherman" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/deps" @@ -739,7 +737,7 @@ Here is the last report for commits in the year 2016. It covers hrev50718-hrev50 func TestRenderStringForRegularPageTranslations(t *testing.T) { c := qt.New(t) b := newTestSitesBuilder(t) - b.WithLogger(loggers.NewBasicLoggerForWriter(jwalterweatherman.LevelError, os.Stderr)) + b.WithLogger(loggers.NewDefault()) b.WithConfigFile("toml", `baseurl = "https://example.org/" @@ -800,7 +798,7 @@ home = ["HTML", "JSON"]`) // Issue 8919 func TestContentProviderWithCustomOutputFormat(t *testing.T) { b := newTestSitesBuilder(t) - b.WithLogger(loggers.NewBasicLoggerForWriter(jwalterweatherman.LevelDebug, os.Stderr)) + b.WithLogger(loggers.NewDefault()) b.WithConfigFile("toml", `baseURL = 'http://example.org/' title = 'My New Hugo Site' @@ -1437,7 +1435,7 @@ Content:{{ .Content }} // https://github.com/gohugoio/hugo/issues/5781 func TestPageWithZeroFile(t *testing.T) { - newTestSitesBuilder(t).WithLogger(loggers.NewWarningLogger()).WithSimpleConfigFile(). + newTestSitesBuilder(t).WithLogger(loggers.NewDefault()).WithSimpleConfigFile(). WithTemplatesAdded("index.html", "{{ .File.Filename }}{{ with .File }}{{ .Dir }}{{ end }}").Build(BuildCfg{}) } diff --git a/hugolib/pagebundler_test.go b/hugolib/pagebundler_test.go index 2ec3718f0..51ce82526 100644 --- a/hugolib/pagebundler_test.go +++ b/hugolib/pagebundler_test.go @@ -20,6 +20,9 @@ import ( "path" "path/filepath" "regexp" + + "github.com/gohugoio/hugo/common/loggers" + "strings" "testing" @@ -31,7 +34,6 @@ import ( "github.com/gohugoio/hugo/hugofs" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/htesting" @@ -93,7 +95,7 @@ func TestPageBundlerSiteRegular(t *testing.T) { c.Assert(err, qt.IsNil) - b := newTestSitesBuilderFromDepsCfg(c, deps.DepsCfg{Logger: loggers.NewErrorLogger(), Fs: fs, Configs: configs}).WithNothingAdded() + b := newTestSitesBuilderFromDepsCfg(c, deps.DepsCfg{Fs: fs, Configs: configs}).WithNothingAdded() b.Build(BuildCfg{}) @@ -1044,7 +1046,7 @@ title: %q } b := newTestSitesBuilder(t).WithConfigFile("toml", config) - b.WithLogger(loggers.NewWarningLogger()) + b.WithLogger(loggers.NewDefault()) b.WithTemplates("_default/list.html", `{{ range .Site.Pages }} {{ .Kind }}|{{ .Path }}|{{ with .CurrentSection }}CurrentSection: {{ .Path }}{{ end }}|{{ .RelPermalink }}{{ end }} @@ -1215,7 +1217,7 @@ title: %q } b := newTestSitesBuilder(t).WithConfigFile("toml", config) - b.WithLogger(loggers.NewWarningLogger()) + b.WithLogger(loggers.NewDefault()) b.WithTemplates("_default/single.html", `{{ range .Resources }} {{ .ResourceType }}|{{ .Title }}| diff --git a/hugolib/pages_capture.go b/hugolib/pages_capture.go index 8a2b875ea..c57c707de 100644 --- a/hugolib/pages_capture.go +++ b/hugolib/pages_capture.go @@ -21,6 +21,7 @@ import ( "reflect" "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/parser/pageparser" @@ -29,7 +30,6 @@ import ( "github.com/gohugoio/hugo/source" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/hugofs" "github.com/spf13/afero" ) diff --git a/hugolib/pages_capture_test.go b/hugolib/pages_capture_test.go index 8c1023a15..c771d30ee 100644 --- a/hugolib/pages_capture_test.go +++ b/hugolib/pages_capture_test.go @@ -53,7 +53,7 @@ func TestPagesCapture(t *testing.T) { t.Run("Collect", func(t *testing.T) { c := qt.New(t) proc := &testPagesCollectorProcessor{} - coll := newPagesCollector(sourceSpec, nil, loggers.NewErrorLogger(), nil, proc) + coll := newPagesCollector(sourceSpec, nil, loggers.NewDefault(), nil, proc) c.Assert(coll.Collect(), qt.IsNil) // 2 bundles, 3 pages. c.Assert(len(proc.items), qt.Equals, 5) diff --git a/hugolib/renderstring_test.go b/hugolib/renderstring_test.go index af66156e6..e0a4cd036 100644 --- a/hugolib/renderstring_test.go +++ b/hugolib/renderstring_test.go @@ -16,6 +16,7 @@ package hugolib import ( "testing" + "github.com/bep/logg" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/common/loggers" ) @@ -80,13 +81,13 @@ func TestRenderStringOnListPage(t *testing.T) { // Issue 9433 func TestRenderStringOnPageNotBackedByAFile(t *testing.T) { t.Parallel() - logger := loggers.NewWarningLogger() + logger := loggers.NewDefault() b := newTestSitesBuilder(t).WithLogger(logger).WithConfigFile("toml", ` disableKinds = ["page", "section", "taxonomy", "term"] `) b.WithTemplates("index.html", `{{ .RenderString "**Hello**" }}`).WithContent("p1.md", "") b.BuildE(BuildCfg{}) - b.Assert(int(logger.LogCounters().WarnCounter.Count()), qt.Equals, 0) + b.Assert(logger.LoggCount(logg.LevelWarn), qt.Equals, 0) } func TestRenderStringWithShortcode(t *testing.T) { diff --git a/hugolib/resource_chain_test.go b/hugolib/resource_chain_test.go index 823f7db45..da90634aa 100644 --- a/hugolib/resource_chain_test.go +++ b/hugolib/resource_chain_test.go @@ -588,7 +588,7 @@ XML: {{ $xml.body }} } t.Parallel() - b := newTestSitesBuilder(t).WithLogger(loggers.NewErrorLogger()) + b := newTestSitesBuilder(t).WithLogger(loggers.NewDefault()) b.WithContent("_index.md", ` --- title: Home diff --git a/hugolib/site.go b/hugolib/site.go index 035f543ee..fca8bca75 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -17,7 +17,6 @@ import ( "context" "fmt" "io" - "log" "mime" "net/url" "path" @@ -27,6 +26,7 @@ import ( "strings" "time" + "github.com/bep/logg" "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/hugio" @@ -285,7 +285,7 @@ func (s *Site) isEnabled(kind string) bool { type siteRefLinker struct { s *Site - errorLogger *log.Logger + errorLogger logg.LevelLogger notFoundURL string } @@ -302,11 +302,11 @@ func newSiteRefLinker(s *Site) (siteRefLinker, error) { func (s siteRefLinker) logNotFound(ref, what string, p page.Page, position text.Position) { if position.IsValid() { - s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q: %s: %s", s.s.Lang(), ref, position.String(), what) + s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q: %s: %s", s.s.Lang(), ref, position.String(), what) } else if p == nil { - s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q: %s", s.s.Lang(), ref, what) + s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q: %s", s.s.Lang(), ref, what) } else { - s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q from page %q: %s", s.s.Lang(), ref, p.Pathc(), what) + s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q from page %q: %s", s.s.Lang(), ref, p.Pathc(), what) } } @@ -507,9 +507,6 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro i18nChanged bool sourceFilesChanged = make(map[string]bool) - - // prevent spamming the log on changes - logger = helpers.NewDistinctErrorLogger() ) var cacheBusters []func(string) bool @@ -531,7 +528,7 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro switch id.Type { case files.ComponentFolderContent: - logger.Println("Source changed", ev) + s.Log.Println("Source changed", ev) sourceChanged = append(sourceChanged, ev) case files.ComponentFolderLayouts: tmplChanged = true @@ -539,16 +536,16 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro tmplAdded = true } if tmplAdded { - logger.Println("Template added", ev) + s.Log.Println("Template added", ev) } else { - logger.Println("Template changed", ev) + s.Log.Println("Template changed", ev) } case files.ComponentFolderData: - logger.Println("Data changed", ev) + s.Log.Println("Data changed", ev) dataChanged = true case files.ComponentFolderI18n: - logger.Println("i18n changed", ev) + s.Log.Println("i18n changed", ev) i18nChanged = true } diff --git a/hugolib/site_new.go b/hugolib/site_new.go index 911414121..c6c5cd2a3 100644 --- a/hugolib/site_new.go +++ b/hugolib/site_new.go @@ -18,10 +18,12 @@ import ( "errors" "fmt" "html/template" + "os" "sort" "time" radix "github.com/armon/go-radix" + "github.com/bep/logg" "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" @@ -100,19 +102,41 @@ func (s *Site) Debug() { func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { conf := cfg.Configs.GetFirstLanguageConfig() - logger := cfg.Logger - if logger == nil { - logger = loggers.NewErrorLogger() + var logger loggers.Logger + if cfg.TestLogger != nil { + logger = cfg.TestLogger + } else { + var logHookLast func(e *logg.Entry) error + if cfg.Configs.Base.PanicOnWarning { + logHookLast = loggers.PanicOnWarningHook + } + if cfg.LogOut == nil { + cfg.LogOut = os.Stdout + } + if cfg.LogLevel == 0 { + cfg.LogLevel = logg.LevelWarn + } + + logOpts := loggers.Options{ + Level: cfg.LogLevel, + Distinct: true, // This will drop duplicate log warning and errors. + HandlerPost: logHookLast, + Stdout: cfg.LogOut, + Stderr: cfg.LogOut, + StoreErrors: conf.Running(), + SuppresssStatements: conf.IgnoredErrors(), + } + logger = loggers.New(logOpts) } - ignorableLogger := loggers.NewIgnorableLogger(logger, conf.IgnoredErrors()) firstSiteDeps := &deps.Deps{ Fs: cfg.Fs, - Log: ignorableLogger, + Log: logger, Conf: conf, TemplateProvider: tplimpl.DefaultTemplateProvider, TranslationProvider: i18n.NewTranslationProvider(), } + if err := firstSiteDeps.Init(); err != nil { return nil, err } @@ -128,7 +152,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { k := language.Lang conf := confm.LanguageConfigMap[k] - frontmatterHandler, err := pagemeta.NewFrontmatterHandler(cfg.Logger, conf.Frontmatter) + frontmatterHandler, err := pagemeta.NewFrontmatterHandler(firstSiteDeps.Log, conf.Frontmatter) if err != nil { return nil, err } @@ -209,6 +233,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { } return h, err + } func newHugoSitesNew(cfg deps.DepsCfg, d *deps.Deps, sites []*Site) (*HugoSites, error) { diff --git a/hugolib/site_render.go b/hugolib/site_render.go index f076b98dd..c4c3f389b 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -181,7 +181,7 @@ func (s *Site) logMissingLayout(name, layout, kind, outputFormat string) { msg += ": " + errMsg - log.Printf(msg, args...) + log.Logf(msg, args...) } // renderPaginator must be run after the owning Page has been rendered. diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 7cf1a5596..e1fd537a8 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -33,6 +33,7 @@ import ( "github.com/fsnotify/fsnotify" "github.com/gohugoio/hugo/common/hexec" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/deps" @@ -47,7 +48,6 @@ import ( "github.com/gohugoio/hugo/resources/resource" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/hugofs" ) @@ -552,7 +552,7 @@ func (s *sitesBuilder) CreateSitesE() error { if depsCfg.Configs.IsZero() { depsCfg.Configs = s.Configs } - depsCfg.Logger = s.logger + depsCfg.TestLogger = s.logger sites, err := NewHugoSites(depsCfg) -- cgit v1.2.3