diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-06-16 08:17:42 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-06-18 13:03:04 +0200 |
commit | 7c9fada778e91976d4ba1cbe942235a9bbeaf5cb (patch) | |
tree | a717f6e0a5915777ae6859564acd13385213bbab | |
parent | 0e7944658660b5658b7640dce3cb346d7198d8c9 (diff) |
Replace the old log setup, with structured logging etc.
Fixes #11124
80 files changed, 1273 insertions, 1082 deletions
diff --git a/cache/filecache/integration_test.go b/cache/filecache/integration_test.go index a59ea048d..a8a45988e 100644 --- a/cache/filecache/integration_test.go +++ b/cache/filecache/integration_test.go @@ -16,11 +16,10 @@ package filecache_test import ( "path/filepath" - jww "github.com/spf13/jwalterweatherman" - "testing" "time" + "github.com/bep/logg" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugolib" @@ -80,7 +79,7 @@ iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAA ` b := hugolib.NewIntegrationTestBuilder( - hugolib.IntegrationTestConfig{T: t, TxtarString: files, Running: true, RunGC: true, NeedsOsFS: true, LogLevel: jww.LevelInfo}, + hugolib.IntegrationTestConfig{T: t, TxtarString: files, Running: true, RunGC: true, NeedsOsFS: true, LogLevel: logg.LevelInfo}, ).Build() b.Assert(b.GCCount, qt.Equals, 0) diff --git a/commands/commandeer.go b/commands/commandeer.go index f7b711973..7322a210a 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -28,12 +28,11 @@ import ( "syscall" "time" - jww "github.com/spf13/jwalterweatherman" - "go.uber.org/automaxprocs/maxprocs" "github.com/bep/clock" "github.com/bep/lazycache" + "github.com/bep/logg" "github.com/bep/overlayfs" "github.com/bep/simplecobra" @@ -114,7 +113,6 @@ type rootCommand struct { baseURL string gc bool poll string - panicOnWarning bool forceSyncStatic bool printPathWarnings bool printUnusedTemplates bool @@ -308,7 +306,7 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo func (r *rootCommand) HugFromConfig(conf *commonConfig) (*hugolib.HugoSites, error) { h, _, err := r.hugoSites.GetOrCreate(r.configVersionID.Load(), func(key int32) (*hugolib.HugoSites, error) { - depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, Logger: r.logger} + depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level()} return hugolib.NewHugoSites(depsCfg) }) return h, err @@ -320,7 +318,7 @@ func (r *rootCommand) Hugo(cfg config.Provider) (*hugolib.HugoSites, error) { if err != nil { return nil, err } - depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, Logger: r.logger} + depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level()} return hugolib.NewHugoSites(depsCfg) }) return h, err @@ -410,7 +408,6 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error { return err } - loggers.PanicOnWarning.Store(r.panicOnWarning) r.commonConfigs = lazycache.New[int32, *commonConfig](lazycache.Options{MaxEntries: 5}) r.hugoSites = lazycache.New[int32, *hugolib.HugoSites](lazycache.Options{MaxEntries: 5}) @@ -418,43 +415,48 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error { } func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) { - var ( - outHandle = r.Out - stdoutThreshold = jww.LevelWarn - ) - - if r.verbose { - helpers.Deprecated("--verbose", "use --logLevel info", false) - stdoutThreshold = jww.LevelInfo - } - - if r.debug { - helpers.Deprecated("--debug", "use --logLevel debug", false) - stdoutThreshold = jww.LevelDebug - } + level := logg.LevelWarn if r.logLevel != "" { switch strings.ToLower(r.logLevel) { case "debug": - stdoutThreshold = jww.LevelDebug + level = logg.LevelDebug case "info": - stdoutThreshold = jww.LevelInfo + level = logg.LevelInfo case "warn", "warning": - stdoutThreshold = jww.LevelWarn + level = logg.LevelWarn case "error": - stdoutThreshold = jww.LevelError + level = logg.LevelError default: return nil, fmt.Errorf("invalid log level: %q, must be one of debug, warn, info or error", r.logLevel) } + } else { + if r.verbose { + helpers.Deprecated("--verbose", "use --logLevel info", false) + level = logg.LevelInfo + } + + if r.debug { + helpers.Deprecated("--debug", "use --logLevel debug", false) + level = logg.LevelDebug + } } - loggers.InitGlobalLogger(stdoutThreshold, jww.LevelWarn, outHandle, io.Discard) - helpers.InitLoggers() - return loggers.NewLogger(stdoutThreshold, jww.LevelWarn, outHandle, io.Discard, running), nil + optsLogger := loggers.Options{ + Distinct: true, + Level: level, + Stdout: r.Out, + Stderr: r.Out, + StoreErrors: running, + } + + return loggers.New(optsLogger), nil + } func (r *rootCommand) Reset() { r.logger.Reset() + loggers.Log().Reset() } // IsTestRun reports whether the command is running as a test. @@ -530,7 +532,7 @@ func applyLocalFlagsBuild(cmd *cobra.Command, r *rootCommand) { cmd.Flags().StringP("layoutDir", "l", "", "filesystem path to layout directory") cmd.Flags().BoolVar(&r.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build") cmd.Flags().StringVar(&r.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes") - cmd.Flags().BoolVar(&r.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log") + cmd.Flags().Bool("panicOnWarning", false, "panic on first WARNING log") cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions") cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") cmd.Flags().BoolVar(&r.forceSyncStatic, "forceSyncStatic", false, "copy all files when static is changed.") diff --git a/commands/deploy.go b/commands/deploy.go index 8dae4bd88..82127adf4 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -52,7 +52,7 @@ documentation. if err != nil { return err } - deployer, err := deploy.New(h.Configs.GetFirstLanguageConfig(), h.PathSpec.PublishFs) + deployer, err := deploy.New(h.Configs.GetFirstLanguageConfig(), h.Log, h.PathSpec.PublishFs) if err != nil { return err } diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go index 95dbb1ca8..60e558c70 100644 --- a/commands/hugobuilder.go +++ b/commands/hugobuilder.go @@ -26,11 +26,13 @@ import ( "sync" "time" + "github.com/bep/logg" "github.com/bep/simplecobra" "github.com/fsnotify/fsnotify" "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/hugo" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/terminal" "github.com/gohugoio/hugo/common/types" @@ -68,7 +70,6 @@ type hugoBuilder struct { onConfigLoaded func(reloaded bool) error fastRenderMode bool - buildWatch bool showErrorInBrowser bool errState hugoBuilderErrState @@ -131,7 +132,7 @@ func (e *hugoBuilderErrState) wasErr() bool { } func (c *hugoBuilder) errCount() int { - return int(c.r.logger.LogCounters().ErrorCounter.Count()) + return c.r.logger.LoggCount(logg.LevelError) + loggers.Log().LoggCount(logg.LevelError) } // getDirList provides NewWatcher() with a list of directories to watch for changes. @@ -363,7 +364,7 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa configFiles = conf.configs.LoadingInfo.ConfigFiles }) - c.r.logger.Println("Watching for config changes in", strings.Join(configFiles, ", ")) + c.r.Println("Watching for config changes in", strings.Join(configFiles, ", ")) for _, configFile := range configFiles { watcher.Add(configFile) configSet[configFile] = true @@ -461,6 +462,7 @@ func (c *hugoBuilder) copyStatic() (map[string]uint64, error) { } func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint64, error) { + infol := c.r.logger.InfoCommand("copy static") publishDir := helpers.FilePathSeparator if sourceFs.PublishFolder != "" { @@ -484,13 +486,13 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint syncer.SrcFs = fs if syncer.Delete { - c.r.logger.Infoln("removing all files from destination that don't exist in static dirs") + infol.Logf("removing all files from destination that don't exist in static dirs") syncer.DeleteFilter = func(f os.FileInfo) bool { |