summaryrefslogtreecommitdiffstats
path: root/commands
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 /commands
parent0e7944658660b5658b7640dce3cb346d7198d8c9 (diff)
Replace the old log setup, with structured logging etc.
Fixes #11124
Diffstat (limited to 'commands')
-rw-r--r--commands/commandeer.go58
-rw-r--r--commands/deploy.go2
-rw-r--r--commands/hugobuilder.go27
-rw-r--r--commands/import.go11
-rw-r--r--commands/server.go19
-rw-r--r--commands/xcommand_template.go79
6 files changed, 57 insertions, 139 deletions
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 {
return f.IsDir() && strings.HasPrefix(f.Name(), ".")
}
}
- c.r.logger.Infoln("syncing static files to", publishDir)
+ infol.Logf("syncing static files to %s", publishDir)
// because we are using a baseFs (to get the union right).
// set sync src to root
@@ -545,14 +547,13 @@ func (c *hugoBuilder) fullBuild(noBuildLock bool) error {
langCount map[string]uint64
)
- if !c.r.quiet {
- fmt.Println("Start building sites … ")
- fmt.Println(hugo.BuildVersionString())
- if terminal.IsTerminal(os.Stdout) {
- defer func() {
- fmt.Print(showCursor + clearLine)
- }()
- }
+ c.r.logger.Println("Start building sites … ")
+ c.r.logger.Println(hugo.BuildVersionString())
+ c.r.logger.Println()
+ if terminal.IsTerminal(os.Stdout) {
+ defer func() {
+ fmt.Print(showCursor + clearLine)
+ }()
}
copyStaticFunc := func() error {
diff --git a/commands/import.go b/commands/import.go
index 30ada15f8..f2c56a9a1 100644
--- a/commands/import.go
+++ b/commands/import.go
@@ -19,12 +19,11 @@ import (
"errors"
"fmt"
"io"
+ "log"
"os"
"path/filepath"
"regexp"
- jww "github.com/spf13/jwalterweatherman"
-
"strconv"
"strings"
"time"
@@ -299,7 +298,7 @@ func (c *importCommand) convertJekyllMetaData(m any, postName string, postDate t
}
func (c *importCommand) convertJekyllPost(path, relPath, targetDir string, draft bool) error {
- jww.TRACE.Println("Converting", path)
+ log.Println("Converting", path)
filename := filepath.Base(path)
postDate, postName, err := c.parseJekyllFilename(filename)
@@ -308,7 +307,7 @@ func (c *importCommand) convertJekyllPost(path, relPath, targetDir string, draft
return nil
}
- jww.TRACE.Println(filename, postDate, postName)
+ log.Println(filename, postDate, postName)
targetFile := filepath.Join(targetDir, relPath)
targetParentDir := filepath.Dir(targetFile)
@@ -367,7 +366,7 @@ func (c *importCommand) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyl
if _, ok := jekyllPostDirs[entry.Name()]; !ok {
err = hugio.CopyDir(fs, sfp, dfp, nil)
if err != nil {
- jww.ERROR.Println(err)
+ c.r.logger.Errorln(err)
}
}
}
@@ -388,7 +387,7 @@ func (c *importCommand) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyl
if !isExcept && entry.Name()[0] != '.' && entry.Name()[0] != '_' {
err = hugio.CopyFile(fs, sfp, dfp)
if err != nil {
- jww.ERROR.Println(err)
+ c.r.logger.Errorln(err)
}
}
}
diff --git a/commands/server.go b/commands/server.go
index 80036618c..7e6ec5b13 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -67,7 +67,6 @@ import (
)
var (
- logErrorRe = regexp.MustCompile(`(?s)ERROR \d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `)
logDuplicateTemplateExecuteRe = regexp.MustCompile(`: template: .*?:\d+:\d+: executing ".*?"`)
logDuplicateTemplateParseRe = regexp.MustCompile(`: template: .*?:\d+:\d*`)
)
@@ -106,9 +105,7 @@ func newServerCommand() *serverCommand {
// Flags.
var uninstall bool
- var c *serverCommand
-
- c = &serverCommand{
+ c := &serverCommand{
quit: make(chan bool),
commands: []simplecobra.Commander{
&simpleCommand{
@@ -654,8 +651,8 @@ func (c *serverCommand) getErrorWithContext() any {
m := make(map[string]any)
- //xwm["Error"] = errors.New(cleanErrorLog(removeErrorPrefixFromLog(c.r.logger.Errors())))
- m["Error"] = errors.New(cleanErrorLog(removeErrorPrefixFromLog(c.r.logger.Errors())))
+ m["Error"] = cleanErrorLog(c.r.logger.Errors())
+
m["Version"] = hugo.BuildVersionString()
ferrors := herrors.UnwrapFileErrorsWithErrorContext(c.errState.buildErr())
m["Files"] = ferrors
@@ -861,6 +858,9 @@ func (c *serverCommand) serve() error {
return err
}
+ // We need the server to share the same logger as the Hugo build (for error counts etc.)
+ c.r.logger = h.Log
+
if isMultiHost {
for _, l := range conf.configs.ConfigLangs() {
baseURLs = append(baseURLs, l.BaseURL().String())
@@ -1066,8 +1066,7 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
}
})
- // prevent spamming the log on changes
- logger := helpers.NewDistinctErrorLogger()
+ logger := s.c.r.logger
for _, ev := range staticEvents {
// Due to our approach of layering both directories and the content's rendered output
@@ -1206,10 +1205,6 @@ func pickOneWriteOrCreatePath(events []fsnotify.Event) string {
return name
}
-func removeErrorPrefixFromLog(content string) string {
- return logErrorRe.ReplaceAllLiteralString(content, "")
-}
-
func formatByteCount(b uint64) string {
const unit = 1000
if b < unit {
diff --git a/commands/xcommand_template.go b/commands/xcommand_template.go
deleted file mode 100644
index eeb9409a0..000000000
--- a/commands/xcommand_template.go
+++ /dev/null
@@ -1,79 +0,0 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package commands
-
-import (
- "context"
- "fmt"
-
- "github.com/bep/simplecobra"
- "github.com/spf13/cobra"
-)
-
-func newSimpleTemplateCommand() simplecobra.Commander {
- return &simpleCommand{
- name: "template",
- run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
-
- return nil
- },
- withc: func(cmd *cobra.Command, r *rootCommand) {
-
- },
- }
-
-}
-
-func newTemplateCommand() *templateCommand {
- return &templateCommand{
- commands: []simplecobra.Commander{},
- }
-
-}
-
-type templateCommand struct {
- r *rootCommand
-
- commands []simplecobra.Commander
-}
-
-func (c *templateCommand) Commands() []simplecobra.Commander {
- return c.commands
-}
-
-func (c *templateCommand) Name() string {
- return "template"
-}
-
-func (c *templateCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
- conf, err := c.r.ConfigFromProvider(c.r.configVersionID.Load(), flagsToCfg(cd, nil))
- if err != nil {
- return err
- }
- fmt.Println("templateCommand.Run", conf)
-
- return nil
-}
-
-func (c *templateCommand) Init(cd *simplecobra.Commandeer) error {
- cmd := cd.CobraCommand
- cmd.Short = "Print the site configuration"
- cmd.Long = `Print the site configuration, both default and custom settings.`
- return nil
-}
-
-func (c *templateCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
- c.r = cd.Root.Command.(*rootCommand)
- return nil
-}