Skip to content

Commit

Permalink
Replace the old log setup, with structured logging etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jun 18, 2023
1 parent aaf2e96 commit ff8215a
Show file tree
Hide file tree
Showing 80 changed files with 1,252 additions and 1,082 deletions.
5 changes: 2 additions & 3 deletions cache/filecache/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
58 changes: 30 additions & 28 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -114,7 +113,6 @@ type rootCommand struct {
baseURL string
gc bool
poll string
panicOnWarning bool
forceSyncStatic bool
printPathWarnings bool
printUnusedTemplates bool
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -410,51 +408,55 @@ 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})

return nil
}

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.
Expand Down Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 14 additions & 13 deletions commands/hugobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -68,7 +70,6 @@ type hugoBuilder struct {
onConfigLoaded func(reloaded bool) error

fastRenderMode bool
buildWatch bool
showErrorInBrowser bool

errState hugoBuilderErrState
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions commands/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"

jww "github.com/spf13/jwalterweatherman"

"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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*`)
)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ff8215a

Please sign in to comment.