Skip to content

Commit 42b9595

Browse files
authored
Merge pull request #2584 from nirs/log-format
Add --log-format json option
2 parents 01eb321 + 357b416 commit 42b9595

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

cmd/limactl/main.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newApp() *cobra.Command {
6060
DisableAutoGenTag: true,
6161
}
6262
rootCmd.PersistentFlags().String("log-level", "", "Set the logging level [trace, debug, info, warn, error]")
63+
rootCmd.PersistentFlags().String("log-format", "text", "Set the logging format [text, json]")
6364
rootCmd.PersistentFlags().Bool("debug", false, "debug mode")
6465
// TODO: "survey" does not support using cygwin terminal on windows yet
6566
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.")
@@ -72,6 +73,24 @@ func newApp() *cobra.Command {
7273
}
7374
logrus.SetLevel(lvl)
7475
}
76+
77+
logFormat, _ := cmd.Flags().GetString("log-format")
78+
switch logFormat {
79+
case "json":
80+
formatter := new(logrus.JSONFormatter)
81+
logrus.StandardLogger().SetFormatter(formatter)
82+
case "text":
83+
// logrus use text format by default.
84+
if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stderr.Fd()) {
85+
formatter := new(logrus.TextFormatter)
86+
// the default setting does not recognize cygwin on windows
87+
formatter.ForceColors = true
88+
logrus.StandardLogger().SetFormatter(formatter)
89+
}
90+
default:
91+
return fmt.Errorf("unsupported log-format: %q", logFormat)
92+
}
93+
7594
debug, _ := cmd.Flags().GetBool("debug")
7695
if debug {
7796
logrus.SetLevel(logrus.DebugLevel)
@@ -83,12 +102,6 @@ func newApp() *cobra.Command {
83102
return errors.New("limactl is running under rosetta, please reinstall lima with native arch")
84103
}
85104

86-
if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
87-
formatter := new(logrus.TextFormatter)
88-
// the default setting does not recognize cygwin on windows
89-
formatter.ForceColors = true
90-
logrus.StandardLogger().SetFormatter(formatter)
91-
}
92105
if os.Geteuid() == 0 && cmd.Name() != "generate-doc" {
93106
return errors.New("must not run as the root user")
94107
}

pkg/progressbar/progressbar.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ import (
66

77
"github.com/cheggaaa/pb/v3"
88
"github.com/mattn/go-isatty"
9+
"github.com/sirupsen/logrus"
910
)
1011

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

1415
bar.Set(pb.Bytes, true)
1516

16-
// Both logrous and pb use stderr by default.
17-
logFd := os.Stderr.Fd()
18-
19-
// Show progress only when logging to terminal.
20-
if isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd) {
17+
if showProgress() {
2118
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
2219
bar.SetRefreshRate(200 * time.Millisecond)
2320
} else {
@@ -31,3 +28,14 @@ func New(size int64) (*pb.ProgressBar, error) {
3128

3229
return bar, nil
3330
}
31+
32+
func showProgress() bool {
33+
// Progress supports only text format fow now.
34+
if _, ok := logrus.StandardLogger().Formatter.(*logrus.TextFormatter); !ok {
35+
return false
36+
}
37+
38+
// Both logrous and pb use stderr by default.
39+
logFd := os.Stderr.Fd()
40+
return isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd)
41+
}

0 commit comments

Comments
 (0)