@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"io/ioutil"
23
23
"os"
24
+ "strings"
24
25
25
26
"github.com/arduino/arduino-cli/cli/board"
26
27
"github.com/arduino/arduino-cli/cli/compile"
51
52
PersistentPreRun : preRun ,
52
53
}
53
54
54
- verbose bool
55
- logFile string
55
+ verbose bool
56
+ logFile string
57
+ logFormat string
56
58
)
57
59
58
60
const (
@@ -80,6 +82,7 @@ func createCliCommandTree(cmd *cobra.Command) {
80
82
cmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , "Print the logs on the standard output." )
81
83
cmd .PersistentFlags ().StringVar (& globals .LogLevel , "log-level" , defaultLogLevel , "Messages with this level and above will be logged (default: warn)." )
82
84
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]." )
83
86
cmd .PersistentFlags ().StringVar (& globals .OutputFormat , "format" , "text" , "The output format, can be [text|json]." )
84
87
cmd .PersistentFlags ().StringVar (& globals .YAMLConfigFile , "config-file" , "" , "The custom config file (if not specified the default will be used)." )
85
88
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) {
111
114
}
112
115
113
116
func preRun (cmd * cobra.Command , args []string ) {
117
+ // normalize the format strings
118
+ globals .OutputFormat = strings .ToLower (globals .OutputFormat )
119
+ logFormat = strings .ToLower (logFormat )
120
+
114
121
// should we log to file?
115
122
if logFile != "" {
116
123
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) {
120
127
}
121
128
122
129
// 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
+ }
124
135
}
125
136
126
137
// should we log to stdout?
@@ -142,15 +153,21 @@ func preRun(cmd *cobra.Command, args []string) {
142
153
logrus .SetLevel (lvl )
143
154
}
144
155
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 {
147
164
feedback .Error ("Invalid output format: " + globals .OutputFormat )
148
165
os .Exit (errorcodes .ErrBadCall )
149
- } else {
150
- // use the format to configure the Feedback
151
- feedback .SetFormat (f )
152
166
}
153
167
168
+ // use the output format to configure the Feedback
169
+ feedback .SetFormat (format )
170
+
154
171
globals .InitConfigs ()
155
172
156
173
logrus .Info (globals .VersionInfo .Application + "-" + globals .VersionInfo .VersionString )
0 commit comments