Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json log format
Browse files Browse the repository at this point in the history
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 <nsoffer@redhat.com>
nirs committed Sep 2, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 791b79a commit 6a4f748
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
@@ -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.")
@@ -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)
@@ -82,12 +101,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.Stderr.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")
}

0 comments on commit 6a4f748

Please sign in to comment.