feat(log): add basic support for logging output

This commit is contained in:
2022-03-21 22:10:21 +00:00
parent 5fcb2b52ab
commit 73f93f8e0b
10 changed files with 119 additions and 12 deletions

View File

@@ -1,9 +1,69 @@
package commands
import "github.com/spf13/cobra"
import (
"io"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
type runEFunc func(cmd *cobra.Command, _ []string) error
func WithPrettyLogging(
f func(cmd *cobra.Command, args []string) error,
) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := SetupZerolog(cmd)
if err != nil {
return err
}
return f(cmd, args)
}
}
func SetupZerolog(cmd *cobra.Command) error {
var levelStr string
if v := os.Getenv("EVM_DEBUG"); v != "" {
levelStr = "debug"
} else if v := os.Getenv("EVM_LOG_LEVEL"); v != "" {
levelStr = v
}
var out io.Writer = os.Stderr
if cmd != nil {
out = cmd.OutOrStderr()
fl := cmd.Flag("log-level")
if fl != nil && (fl.Changed || levelStr == "") {
levelStr = fl.Value.String()
}
}
if levelStr == "" {
levelStr = "info"
}
level, err := zerolog.ParseLevel(levelStr)
if err != nil {
return err
}
zerolog.SetGlobalLevel(level)
zerolog.TimeFieldFormat = ""
output := zerolog.ConsoleWriter{Out: out}
output.FormatTimestamp = func(i interface{}) string {
return ""
}
log.Logger = zerolog.New(output).With().Timestamp().Logger()
return nil
}
type validArgsFunc func(
cmd *cobra.Command,
args []string,

View File

@@ -11,7 +11,7 @@ func NewConfig(mgr *manager.Manager) (*cobra.Command, error) {
Short: "Show evm environment/setup details",
Aliases: []string{"env", "info"},
ValidArgs: []string{},
RunE: configRunE(mgr),
RunE: WithPrettyLogging(configRunE(mgr)),
}
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")

View File

@@ -11,6 +11,11 @@ func NewEvm(mgr *manager.Manager) (*cobra.Command, error) {
Short: "A simple and opinionated Emacs Version Manager and build tool",
}
cmd.PersistentFlags().StringP(
"log-level", "l", "info",
"one of: trace, debug, info, warn, error, fatal, panic",
)
configCmd, err := NewConfig(mgr)
if err != nil {
return nil, err

View File

@@ -22,7 +22,7 @@ func NewExec(mgr *manager.Manager) (*cobra.Command, error) {
DisableFlagsInUseLine: true,
Hidden: true,
ValidArgsFunction: execValidArgs(mgr),
RunE: execRunE(mgr),
RunE: WithPrettyLogging(execRunE(mgr)),
}
return cmd, nil

View File

@@ -15,7 +15,7 @@ func NewList(mgr *manager.Manager) (*cobra.Command, error) {
Aliases: []string{"ls", "versions"},
Args: cobra.ExactArgs(0),
ValidArgsFunction: noValidArgs,
RunE: listRunE(mgr),
RunE: WithPrettyLogging(listRunE(mgr)),
}
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")

View File

@@ -13,7 +13,7 @@ func NewRehash(mgr *manager.Manager) (*cobra.Command, error) {
Short: "Update shims for all or specific versions",
Aliases: []string{"reshim"},
ValidArgsFunction: rehashValidArgs(mgr),
RunE: rehashRunE(mgr),
RunE: WithPrettyLogging(rehashRunE(mgr)),
}
return cmd, nil

View File

@@ -14,7 +14,7 @@ func NewUse(mgr *manager.Manager) (*cobra.Command, error) {
Aliases: []string{"activate", "switch"},
Args: cobra.ExactArgs(1),
ValidArgsFunction: useValidArgs(mgr),
RunE: useRunE(mgr),
RunE: WithPrettyLogging(useRunE(mgr)),
}
return cmd, nil