Skip to content

Commit 9e6371a

Browse files
author
Massimiliano Pippi
committed
add an option to format logs with JSON
1 parent 3e0b555 commit 9e6371a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

cli/cli.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"strings"
2425

2526
"github.com/arduino/arduino-cli/cli/board"
2627
"github.com/arduino/arduino-cli/cli/compile"
@@ -51,8 +52,9 @@ var (
5152
PersistentPreRun: preRun,
5253
}
5354

54-
verbose bool
55-
logFile string
55+
verbose bool
56+
logFile string
57+
logFormat string
5658
)
5759

5860
const (
@@ -80,6 +82,7 @@ func createCliCommandTree(cmd *cobra.Command) {
8082
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")
8183
cmd.PersistentFlags().StringVar(&globals.LogLevel, "log-level", defaultLogLevel, "Messages with this level and above will be logged (default: warn).")
8284
cmd.PersistentFlags().StringVar(&logFile, "log-file", "", "Path to the file where logs will be written.")
85+
cmd.PersistentFlags().StringVar(&logFormat, "log-format", "text", "The output format for the logs, can be [text|json].")
8386
cmd.PersistentFlags().StringVar(&globals.OutputFormat, "format", "text", "The output format, can be [text|json].")
8487
cmd.PersistentFlags().StringVar(&globals.YAMLConfigFile, "config-file", "", "The custom config file (if not specified the default will be used).")
8588
cmd.PersistentFlags().StringSliceVar(&globals.AdditionalUrls, "additional-urls", []string{}, "Additional URLs for the board manager.")
@@ -102,6 +105,7 @@ func toLogLevel(s string) (t logrus.Level, found bool) {
102105
}
103106

104107
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
108+
arg = strings.ToLower(arg)
105109
f, found := map[string]feedback.OutputFormat{
106110
"json": feedback.JSON,
107111
"text": feedback.Text,
@@ -151,6 +155,11 @@ func preRun(cmd *cobra.Command, args []string) {
151155
feedback.SetFormat(f)
152156
}
153157

158+
// set the Logger format
159+
if strings.ToLower(logFormat) == "json" {
160+
logrus.SetFormatter(&logrus.JSONFormatter{})
161+
}
162+
154163
globals.InitConfigs()
155164

156165
logrus.Info(globals.VersionInfo.Application + "-" + globals.VersionInfo.VersionString)

0 commit comments

Comments
 (0)