Skip to content

Commit 5cbac54

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"} ... Discussed in #2576 Fixes #2583 Signed-off-by: Nir Soffer <[email protected]>
1 parent 547e45d commit 5cbac54

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
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.Stderr.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
}

0 commit comments

Comments
 (0)