Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --log-format json option #2584

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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":
nirs marked this conversation as resolved.
Show resolved Hide resolved
// 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)
}
Loading