@@ -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"
5152 PersistentPreRun : preRun ,
5253 }
5354
54- verbose bool
55- logFile string
55+ verbose bool
56+ logFile string
57+ logFormat string
5658)
5759
5860const (
@@ -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." )
@@ -111,6 +114,10 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
111114}
112115
113116func preRun (cmd * cobra.Command , args []string ) {
117+ // normalize the format strings
118+ globals .OutputFormat = strings .ToLower (globals .OutputFormat )
119+ logFormat = strings .ToLower (logFormat )
120+
114121 // should we log to file?
115122 if logFile != "" {
116123 file , err := os .OpenFile (logFile , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0666 )
@@ -120,7 +127,11 @@ func preRun(cmd *cobra.Command, args []string) {
120127 }
121128
122129 // we use a hook so we don't get color codes in the log file
123- logrus .AddHook (lfshook .NewHook (file , & logrus.TextFormatter {}))
130+ if logFormat == "json" {
131+ logrus .AddHook (lfshook .NewHook (file , & logrus.JSONFormatter {}))
132+ } else {
133+ logrus .AddHook (lfshook .NewHook (file , & logrus.TextFormatter {}))
134+ }
124135 }
125136
126137 // should we log to stdout?
@@ -142,15 +153,21 @@ func preRun(cmd *cobra.Command, args []string) {
142153 logrus .SetLevel (lvl )
143154 }
144155
145- // check the right format was passed
146- if f , found := parseFormatString (globals .OutputFormat ); ! found {
156+ // set the Logger format
157+ if logFormat == "json" {
158+ logrus .SetFormatter (& logrus.JSONFormatter {})
159+ }
160+
161+ // check the right output format was passed
162+ format , found := parseFormatString (globals .OutputFormat )
163+ if ! found {
147164 feedback .Error ("Invalid output format: " + globals .OutputFormat )
148165 os .Exit (errorcodes .ErrBadCall )
149- } else {
150- // use the format to configure the Feedback
151- feedback .SetFormat (f )
152166 }
153167
168+ // use the output format to configure the Feedback
169+ feedback .SetFormat (format )
170+
154171 globals .InitConfigs ()
155172
156173 logrus .Info (globals .VersionInfo .Application + "-" + globals .VersionInfo .VersionString )
0 commit comments