commands: Remove flags log, verboseLog, add flag logLevel, deprecate flags verbose and debug

Fixes #9648
Fixes #11071
This commit is contained in:
Bjørn Erik Pedersen 2023-06-12 15:50:53 +02:00
parent 489519566e
commit ef147f4e87

View file

@ -22,6 +22,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
@ -125,17 +126,16 @@ type rootCommand struct {
traceprofile string traceprofile string
printm bool printm bool
// TODO(bep) var vs string logLevel string
logging bool
verbose bool verbose bool
verboseLog bool
debug bool debug bool
quiet bool quiet bool
renderToMemory bool renderToMemory bool
cfgFile string cfgFile string
cfgDir string cfgDir string
logFile string
} }
func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) { func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
@ -419,43 +419,38 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) { func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) {
var ( var (
logHandle = io.Discard
logThreshold = jww.LevelWarn
outHandle = r.Out outHandle = r.Out
stdoutThreshold = jww.LevelWarn stdoutThreshold = jww.LevelWarn
) )
if r.verboseLog || r.logging || (r.logFile != "") { if r.verbose {
var err error helpers.Deprecated("--verbose", "use --logLevel info", false)
if r.logFile != "" {
logHandle, err = os.OpenFile(r.logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return nil, fmt.Errorf("Failed to open log file %q: %s", r.logFile, err)
}
} else {
logHandle, err = os.CreateTemp("", "hugo")
if err != nil {
return nil, err
}
}
} else if r.verbose {
stdoutThreshold = jww.LevelInfo stdoutThreshold = jww.LevelInfo
} }
if r.debug { if r.debug {
helpers.Deprecated("--debug", "use --logLevel debug", false)
stdoutThreshold = jww.LevelDebug stdoutThreshold = jww.LevelDebug
} }
if r.verboseLog { if r.logLevel != "" {
logThreshold = jww.LevelInfo switch strings.ToLower(r.logLevel) {
if r.debug { case "debug":
logThreshold = jww.LevelDebug stdoutThreshold = jww.LevelDebug
case "info":
stdoutThreshold = jww.LevelInfo
case "warn", "warning":
stdoutThreshold = jww.LevelWarn
case "error":
stdoutThreshold = jww.LevelError
default:
return nil, fmt.Errorf("invalid log level: %q, must be one of debug, warn, info or error", r.logLevel)
} }
} }
loggers.InitGlobalLogger(stdoutThreshold, logThreshold, outHandle, logHandle) loggers.InitGlobalLogger(stdoutThreshold, jww.LevelWarn, outHandle, io.Discard)
helpers.InitLoggers() helpers.InitLoggers()
return loggers.NewLogger(stdoutThreshold, logThreshold, outHandle, logHandle, running), nil return loggers.NewLogger(stdoutThreshold, jww.LevelWarn, outHandle, io.Discard, running), nil
} }
func (r *rootCommand) Reset() { func (r *rootCommand) Reset() {
@ -498,15 +493,10 @@ Complete documentation is available at https://gohugo.io/.`
cmd.PersistentFlags().BoolVarP(&r.verbose, "verbose", "v", false, "verbose output") cmd.PersistentFlags().BoolVarP(&r.verbose, "verbose", "v", false, "verbose output")
cmd.PersistentFlags().BoolVarP(&r.debug, "debug", "", false, "debug output") cmd.PersistentFlags().BoolVarP(&r.debug, "debug", "", false, "debug output")
cmd.PersistentFlags().BoolVar(&r.logging, "log", false, "enable Logging") cmd.PersistentFlags().StringVar(&r.logLevel, "logLevel", "", "log level (debug|info|warn|error)")
cmd.PersistentFlags().StringVar(&r.logFile, "logFile", "", "log File path (if set, logging enabled automatically)")
cmd.PersistentFlags().BoolVar(&r.verboseLog, "verboseLog", false, "verbose logging")
cmd.Flags().BoolVarP(&r.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") cmd.Flags().BoolVarP(&r.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
cmd.Flags().BoolVar(&r.renderToMemory, "renderToMemory", false, "render to memory (only useful for benchmark testing)") cmd.Flags().BoolVar(&r.renderToMemory, "renderToMemory", false, "render to memory (only useful for benchmark testing)")
// Set bash-completion
_ = cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
// Configure local flags // Configure local flags
applyLocalFlagsBuild(cmd, r) applyLocalFlagsBuild(cmd, r)