Skip to content

Commit

Permalink
Merge pull request #2584 from nirs/log-format
Browse files Browse the repository at this point in the history
Add --log-format json option
  • Loading branch information
jandubois authored Sep 3, 2024
2 parents 01eb321 + 357b416 commit 42b9595
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
25 changes: 19 additions & 6 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newApp() *cobra.Command {
DisableAutoGenTag: true,
}
rootCmd.PersistentFlags().String("log-level", "", "Set the logging level [trace, debug, info, warn, error]")
rootCmd.PersistentFlags().String("log-format", "text", "Set the logging format [text, json]")
rootCmd.PersistentFlags().Bool("debug", false, "debug mode")
// TODO: "survey" does not support using cygwin terminal on windows yet
rootCmd.PersistentFlags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation.")
Expand All @@ -72,6 +73,24 @@ func newApp() *cobra.Command {
}
logrus.SetLevel(lvl)
}

logFormat, _ := cmd.Flags().GetString("log-format")
switch logFormat {
case "json":
formatter := new(logrus.JSONFormatter)
logrus.StandardLogger().SetFormatter(formatter)
case "text":
// logrus use text format by default.
if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stderr.Fd()) {
formatter := new(logrus.TextFormatter)
// the default setting does not recognize cygwin on windows
formatter.ForceColors = true
logrus.StandardLogger().SetFormatter(formatter)
}
default:
return fmt.Errorf("unsupported log-format: %q", logFormat)
}

debug, _ := cmd.Flags().GetBool("debug")
if debug {
logrus.SetLevel(logrus.DebugLevel)
Expand All @@ -83,12 +102,6 @@ func newApp() *cobra.Command {
return errors.New("limactl is running under rosetta, please reinstall lima with native arch")
}

if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
formatter := new(logrus.TextFormatter)
// the default setting does not recognize cygwin on windows
formatter.ForceColors = true
logrus.StandardLogger().SetFormatter(formatter)
}
if os.Geteuid() == 0 && cmd.Name() != "generate-doc" {
return errors.New("must not run as the root user")
}
Expand Down
18 changes: 13 additions & 5 deletions pkg/progressbar/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import (

"github.com/cheggaaa/pb/v3"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
)

func New(size int64) (*pb.ProgressBar, error) {
bar := pb.New64(size)

bar.Set(pb.Bytes, true)

// Both logrous and pb use stderr by default.
logFd := os.Stderr.Fd()

// Show progress only when logging to terminal.
if isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd) {
if showProgress() {
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
bar.SetRefreshRate(200 * time.Millisecond)
} else {
Expand All @@ -31,3 +28,14 @@ func New(size int64) (*pb.ProgressBar, error) {

return bar, nil
}

func showProgress() bool {
// Progress supports only text format fow now.
if _, ok := logrus.StandardLogger().Formatter.(*logrus.TextFormatter); !ok {
return false
}

// Both logrous and pb use stderr by default.
logFd := os.Stderr.Fd()
return isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd)
}

0 comments on commit 42b9595

Please sign in to comment.