diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-06-16 08:17:42 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-06-18 13:03:04 +0200 |
commit | 7c9fada778e91976d4ba1cbe942235a9bbeaf5cb (patch) | |
tree | a717f6e0a5915777ae6859564acd13385213bbab /resources | |
parent | 0e7944658660b5658b7640dce3cb346d7198d8c9 (diff) | |
download | hugo-7c9fada778e91976d4ba1cbe942235a9bbeaf5cb.tar.gz hugo-7c9fada778e91976d4ba1cbe942235a9bbeaf5cb.zip |
Replace the old log setup, with structured logging etc.
Fixes #11124
Diffstat (limited to 'resources')
10 files changed, 52 insertions, 47 deletions
diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go index d827bfbad..98ab6b222 100644 --- a/resources/page/pagemeta/page_frontmatter.go +++ b/resources/page/pagemeta/page_frontmatter.go @@ -18,9 +18,9 @@ import ( "time" "github.com/gohugoio/hugo/common/htime" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/paths" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/resources/resource" @@ -270,7 +270,7 @@ func toLowerSlice(in any) []string { // If no logger is provided, one will be created. func NewFrontmatterHandler(logger loggers.Logger, frontMatterConfig FrontmatterConfig) (FrontMatterHandler, error) { if logger == nil { - logger = loggers.NewErrorLogger() + logger = loggers.NewDefault() } allDateKeys := make(map[string]bool) diff --git a/resources/page/testhelpers_page_test.go b/resources/page/testhelpers_page_test.go index c462e176f..95124cb58 100644 --- a/resources/page/testhelpers_page_test.go +++ b/resources/page/testhelpers_page_test.go @@ -30,7 +30,7 @@ func newTestPathSpecFor(cfg config.Provider) *helpers.PathSpec { mfs := afero.NewMemMapFs() conf := testconfig.GetTestConfig(mfs, cfg) fs := hugofs.NewFrom(mfs, conf.BaseConfig()) - ps, err := helpers.NewPathSpec(fs, conf, loggers.NewErrorLogger()) + ps, err := helpers.NewPathSpec(fs, conf, loggers.NewDefault()) if err != nil { panic(err) } diff --git a/resources/resource_spec.go b/resources/resource_spec.go index 5ecb021fe..3e1b53205 100644 --- a/resources/resource_spec.go +++ b/resources/resource_spec.go @@ -31,6 +31,7 @@ import ( "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/hexec" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/identity" @@ -39,7 +40,6 @@ import ( "github.com/gohugoio/hugo/resources/postpub" "github.com/gohugoio/hugo/cache/filecache" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/resources/images" "github.com/gohugoio/hugo/resources/page" @@ -75,7 +75,7 @@ func NewSpec( } if logger == nil { - logger = loggers.NewErrorLogger() + logger = loggers.NewDefault() } permalinks, err := page.NewPermalinkExpander(s.URLize, conf.Permalinks) diff --git a/resources/resource_transformers/babel/babel.go b/resources/resource_transformers/babel/babel.go index ff19d9dda..5f8fcb00f 100644 --- a/resources/resource_transformers/babel/babel.go +++ b/resources/resource_transformers/babel/babel.go @@ -122,10 +122,10 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx } var configFile string - logger := t.rs.Logger + infol := t.rs.Logger.InfoCommand(binaryName) + infoW := loggers.LevelLoggerToWriter(infol) var errBuf bytes.Buffer - infoW := loggers.LoggerToWriterWithPrefix(logger.Info(), "babel") if t.options.Config != "" { configFile = t.options.Config @@ -149,7 +149,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx var cmdArgs []any if configFile != "" { - logger.Infoln("babel: use config file", configFile) + infol.Logf("use config file %q", configFile) cmdArgs = []any{"--config-file", configFile} } diff --git a/resources/resource_transformers/babel/integration_test.go b/resources/resource_transformers/babel/integration_test.go index 164e7fd40..44a13f103 100644 --- a/resources/resource_transformers/babel/integration_test.go +++ b/resources/resource_transformers/babel/integration_test.go @@ -16,8 +16,7 @@ package babel_test import ( "testing" - jww "github.com/spf13/jwalterweatherman" - + "github.com/bep/logg" "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugolib" ) @@ -82,7 +81,7 @@ Transpiled3: {{ $transpiled.Permalink }} TxtarString: files, NeedsOsFS: true, NeedsNpmInstall: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, }).Build() b.AssertLogContains("babel: Hugo Environment: production") diff --git a/resources/resource_transformers/postcss/integration_test.go b/resources/resource_transformers/postcss/integration_test.go index cfe5f8a2c..c920fe17d 100644 --- a/resources/resource_transformers/postcss/integration_test.go +++ b/resources/resource_transformers/postcss/integration_test.go @@ -16,11 +16,11 @@ package postcss_test import ( "fmt" "path/filepath" + "runtime" "strings" "testing" - jww "github.com/spf13/jwalterweatherman" - + "github.com/bep/logg" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugofs" @@ -124,7 +124,7 @@ func TestTransformPostCSS(t *testing.T) { T: c, NeedsOsFS: true, NeedsNpmInstall: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, WorkingDir: tempDir, TxtarString: files, }).Build() @@ -148,6 +148,11 @@ func TestTransformPostCSSError(t *testing.T) { t.Skip("Skip long running test when running locally") } + if runtime.GOOS == "windows" { + //TODO(bep) This has started to fail on Windows with Go 1.19 on GitHub Actions for some mysterious reason. + t.Skip("Skip on Windows") + } + c := qt.New(t) s, err := hugolib.NewIntegrationTestBuilder( @@ -176,7 +181,7 @@ func TestTransformPostCSSImportError(t *testing.T) { T: c, NeedsOsFS: true, NeedsNpmInstall: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, TxtarString: strings.ReplaceAll(postCSSIntegrationTestFiles, `@import "components/all.css";`, `@import "components/doesnotexist.css";`), }).BuildE() @@ -201,7 +206,7 @@ func TestTransformPostCSSImporSkipInlineImportsNotFound(t *testing.T) { T: c, NeedsOsFS: true, NeedsNpmInstall: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, TxtarString: files, }).Build() @@ -233,7 +238,7 @@ func TestTransformPostCSSResourceCacheWithPathInBaseURL(t *testing.T) { T: c, NeedsOsFS: true, NeedsNpmInstall: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, TxtarString: files, WorkingDir: tempDir, }).Build() diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go index 376d72182..083607246 100644 --- a/resources/resource_transformers/postcss/postcss.go +++ b/resources/resource_transformers/postcss/postcss.go @@ -27,13 +27,12 @@ import ( "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/hexec" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/common/hugo" - "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/resources/internal" "github.com/spf13/afero" "github.com/spf13/cast" @@ -151,10 +150,12 @@ func (t *postcssTransformation) Key() internal.ResourceTransformationKey { func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationCtx) error { const binaryName = "postcss" + infol := t.rs.Logger.InfoCommand(binaryName) + infoW := loggers.LevelLoggerToWriter(infol) + ex := t.rs.ExecHelper var configFile string - logger := t.rs.Logger var options Options if t.optionsm != nil { @@ -185,7 +186,7 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC var cmdArgs []any if configFile != "" { - logger.Infoln("postcss: use config file", configFile) + infol.Logf("use config file %q", configFile) cmdArgs = []any{"--config", configFile} } @@ -194,7 +195,6 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC } var errBuf bytes.Buffer - infoW := loggers.LoggerToWriterWithPrefix(logger.Info(), "postcss") stderr := io.MultiWriter(infoW, &errBuf) cmdArgs = append(cmdArgs, hexec.WithStderr(stderr)) @@ -401,7 +401,6 @@ func (imp *importResolver) shouldImport(s string) bool { } func (imp *importResolver) toFileError(output string) error { - output = strings.TrimSpace(loggers.RemoveANSIColours(output)) inErr := errors.New(output) match := cssSyntaxErrorRe.FindStringSubmatch(output) diff --git a/resources/resource_transformers/postcss/postcss_test.go b/resources/resource_transformers/postcss/postcss_test.go index 6901d69de..dd0695cd1 100644 --- a/resources/resource_transformers/postcss/postcss_test.go +++ b/resources/resource_transformers/postcss/postcss_test.go @@ -18,9 +18,9 @@ import ( "strings" "testing" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/htesting/hqt" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/helpers" "github.com/spf13/afero" @@ -95,7 +95,7 @@ LOCAL_STYLE mainStyles, "styles.css", Options{}, - fs, loggers.NewErrorLogger(), + fs, loggers.NewDefault(), ) r, err := imp.resolve() @@ -144,7 +144,7 @@ LOCAL_STYLE @import "e.css"; @import "missing.css";` - logger := loggers.NewErrorLogger() + logger := loggers.NewDefault() for i := 0; i < b.N; i++ { b.StopTimer() diff --git a/resources/resource_transformers/tocss/dartsass/client.go b/resources/resource_transformers/tocss/dartsass/client.go index 9ae317886..63278f0db 100644 --- a/resources/resource_transformers/tocss/dartsass/client.go +++ b/resources/resource_transformers/tocss/dartsass/client.go @@ -58,6 +58,8 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) transpiler *godartsass.Transpiler transpilerv1 *godartsassv1.Transpiler err error + infol = rs.Logger.InfoCommand("Dart Sass") + warnl = rs.Logger.WarnCommand("Dart Sass") ) if hugo.IsDartSassV2() { @@ -68,10 +70,10 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) switch event.Type { case godartsass.LogEventTypeDebug: // Log as Info for now, we may adjust this if it gets too chatty. - rs.Logger.Infof("Dart Sass: %s", message) + infol.Logf(message) default: // The rest are either deprecations or @warn statements. - rs.Logger.Warnf("Dart Sass: %s", message) + warnl.Logf(message) } }, }) @@ -84,10 +86,10 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) switch event.Type { case godartsassv1.LogEventTypeDebug: // Log as Info for now, we may adjust this if it gets too chatty. - rs.Logger.Infof("Dart Sass: %s", message) + infol.Logf(message) default: // The rest are either deprecations or @warn statements. - rs.Logger.Warnf("Dart Sass: %s", message) + warnl.Logf(message) } }, }) diff --git a/resources/resource_transformers/tocss/dartsass/integration_test.go b/resources/resource_transformers/tocss/dartsass/integration_test.go index d76592336..c370a1cc8 100644 --- a/resources/resource_transformers/tocss/dartsass/integration_test.go +++ b/resources/resource_transformers/tocss/dartsass/integration_test.go @@ -17,10 +17,10 @@ import ( "strings" "testing" + "github.com/bep/logg" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass" - jww "github.com/spf13/jwalterweatherman" ) func TestTransformIncludePaths(t *testing.T) { @@ -288,11 +288,11 @@ T1: {{ $r.Content }} T: t, TxtarString: files, NeedsOsFS: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, }).Build() - b.AssertLogMatches(`WARN.*Dart Sass: foo`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:1:0: bar`) + b.AssertLogMatches(`Dart Sass: foo`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:1:0: bar`) } @@ -513,20 +513,20 @@ T1: {{ $r.Content }} T: t, TxtarString: files, NeedsOsFS: true, - LogLevel: jww.LevelInfo, + LogLevel: logg.LevelInfo, }).Build() - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:3:0: color`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:4:0: color`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:5:0: color`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:6:0: number`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:7:0: number`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:8:0: number`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:9:0: string`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:10:0: string`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:11:0: string`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:12:0: number`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:13:0: number`) - b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:14:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:3:0: color`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:4:0: color`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:5:0: color`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:6:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:7:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:8:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:9:0: string`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:10:0: string`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:11:0: string`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:12:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:13:0: number`) + b.AssertLogMatches(`Dart Sass: .*assets.*main.scss:14:0: number`) } |