@@ -57,16 +57,10 @@ var (
57
57
}
58
58
59
59
verbose bool
60
- logFile string
61
- logFormat string
62
60
outputFormat string
63
61
configFile string
64
62
)
65
63
66
- const (
67
- defaultLogLevel = "info"
68
- )
69
-
70
64
// Init the cobra root command
71
65
func init () {
72
66
createCliCommandTree (ArduinoCli )
@@ -86,9 +80,12 @@ func createCliCommandTree(cmd *cobra.Command) {
86
80
cmd .AddCommand (version .NewCommand ())
87
81
88
82
cmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , "Print the logs on the standard output." )
89
- cmd .PersistentFlags ().StringVar (& globals .LogLevel , "log-level" , defaultLogLevel , "Messages with this level and above will be logged." )
90
- cmd .PersistentFlags ().StringVar (& logFile , "log-file" , "" , "Path to the file where logs will be written." )
91
- cmd .PersistentFlags ().StringVar (& logFormat , "log-format" , "text" , "The output format for the logs, can be [text|json]." )
83
+ cmd .PersistentFlags ().String ("log-level" , "" , "Messages with this level and above will be logged." )
84
+ viper .BindPFlag ("logging.level" , cmd .PersistentFlags ().Lookup ("log-level" ))
85
+ cmd .PersistentFlags ().String ("log-file" , "" , "Path to the file where logs will be written." )
86
+ viper .BindPFlag ("logging.file" , cmd .PersistentFlags ().Lookup ("log-file" ))
87
+ cmd .PersistentFlags ().String ("log-format" , "" , "The output format for the logs, can be [text|json]." )
88
+ viper .BindPFlag ("logging.format" , cmd .PersistentFlags ().Lookup ("log-format" ))
92
89
cmd .PersistentFlags ().StringVar (& outputFormat , "format" , "text" , "The output format, can be [text|json]." )
93
90
cmd .PersistentFlags ().StringVar (& configFile , "config-file" , "" , "The custom config file (if not specified the default will be used)." )
94
91
cmd .PersistentFlags ().StringSlice ("additional-urls" , []string {}, "Additional URLs for the board manager." )
@@ -121,13 +118,34 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
121
118
}
122
119
123
120
func preRun (cmd * cobra.Command , args []string ) {
121
+ // before doing anything, decide whether we should log to stdout
122
+ if verbose {
123
+ // if we print on stdout, do it in full colors
124
+ logrus .SetOutput (colorable .NewColorableStdout ())
125
+ logrus .SetFormatter (& logrus.TextFormatter {
126
+ ForceColors : true ,
127
+ })
128
+ } else {
129
+ logrus .SetOutput (ioutil .Discard )
130
+ }
131
+
132
+ // setup the configuration system
133
+ configPath := ""
134
+ if configFile != "" {
135
+ // override the config path if --config-file was passed
136
+ configPath = filepath .Dir (configFile )
137
+ }
138
+ configuration .Init (configPath )
139
+
124
140
// normalize the format strings
125
141
outputFormat = strings .ToLower (outputFormat )
126
142
// configure the output package
127
143
output .OutputFormat = outputFormat
128
- logFormat = strings .ToLower (logFormat )
144
+ // configure log format
145
+ logFormat := strings .ToLower (viper .GetString ("logging.format" ))
129
146
130
147
// should we log to file?
148
+ logFile := viper .GetString ("log.file" )
131
149
if logFile != "" {
132
150
file , err := os .OpenFile (logFile , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0666 )
133
151
if err != nil {
@@ -143,20 +161,9 @@ func preRun(cmd *cobra.Command, args []string) {
143
161
}
144
162
}
145
163
146
- // should we log to stdout?
147
- if verbose {
148
- logrus .SetOutput (colorable .NewColorableStdout ())
149
- logrus .SetFormatter (& logrus.TextFormatter {
150
- ForceColors : true ,
151
- })
152
- } else {
153
- // Discard logrus output if no writer was set
154
- logrus .SetOutput (ioutil .Discard )
155
- }
156
-
157
164
// configure logging filter
158
- if lvl , found := toLogLevel (globals . LogLevel ); ! found {
159
- fmt . Printf ("Invalid option for --log-level: %s" , globals . LogLevel )
165
+ if lvl , found := toLogLevel (viper . GetString ( "logging.level" ) ); ! found {
166
+ feedback . Errorf ("Invalid option for --log-level: %s" , viper . GetString ( "logging.level" ) )
160
167
os .Exit (errorcodes .ErrBadArgument )
161
168
} else {
162
169
logrus .SetLevel (lvl )
@@ -177,13 +184,6 @@ func preRun(cmd *cobra.Command, args []string) {
177
184
// use the output format to configure the Feedback
178
185
feedback .SetFormat (format )
179
186
180
- // override the config path if --config-file was passed
181
- configPath := ""
182
- if configFile != "" {
183
- configPath = filepath .Dir (configFile )
184
- }
185
- configuration .Init (configPath )
186
-
187
187
logrus .Info (globals .VersionInfo .Application + "-" + globals .VersionInfo .VersionString )
188
188
logrus .Info ("Starting root command preparation (`arduino`)" )
189
189
0 commit comments