Skip to content

Commit f137b70

Browse files
committed
Add json log format
Add an option to to format logs in json format. This can be useful for programs running limactl that want to consume some content from the logs in the program own logs. Example: % _output/bin/limactl -h ... Flags: --debug debug mode -h, --help help for limactl --log-format string Set the logging format [text, json] (default "text") --log-level string Set the logging level [trace, debug, info, warn, error] ... % _output/bin/limactl start --log-format json --plain --vm-type vz --tty=false {"level":"info","msg":"Terminal is not available, proceeding without opening an editor","time":"2024-09-02T05:12:28+03:00"} {"level":"info","msg":"Starting the instance \"default\" with VM driver \"vz\"","time":"2024-09-02T05:12:28+03:00"} ... Related changes included: - Show progress only when using text format. When we have json format for the progress we can enable it. - Fix terminal check for windows, was using os.Stdout when we log to os.Stderr. Discussed in #2576 Fixes #2583 Signed-off-by: Nir Soffer <[email protected]>
1 parent 01eb321 commit f137b70

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

cmd/limactl/main.go

Lines changed: 19 additions & 6 deletions
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

Lines changed: 13 additions & 5 deletions
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 forma 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)