Skip to content

Commit 2942748

Browse files
author
Massimiliano Pippi
committed
map log settings to config file
1 parent b64069c commit 2942748

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

Diff for: cli/cli.go

+30-30
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,10 @@ var (
5757
}
5858

5959
verbose bool
60-
logFile string
61-
logFormat string
6260
outputFormat string
6361
configFile string
6462
)
6563

66-
const (
67-
defaultLogLevel = "info"
68-
)
69-
7064
// Init the cobra root command
7165
func init() {
7266
createCliCommandTree(ArduinoCli)
@@ -86,9 +80,12 @@ func createCliCommandTree(cmd *cobra.Command) {
8680
cmd.AddCommand(version.NewCommand())
8781

8882
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"))
9289
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", "The output format, can be [text|json].")
9390
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", "The custom config file (if not specified the default will be used).")
9491
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, "Additional URLs for the board manager.")
@@ -121,13 +118,34 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
121118
}
122119

123120
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+
124140
// normalize the format strings
125141
outputFormat = strings.ToLower(outputFormat)
126142
// configure the output package
127143
output.OutputFormat = outputFormat
128-
logFormat = strings.ToLower(logFormat)
144+
// configure log format
145+
logFormat := strings.ToLower(viper.GetString("logging.format"))
129146

130147
// should we log to file?
148+
logFile := viper.GetString("log.file")
131149
if logFile != "" {
132150
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
133151
if err != nil {
@@ -143,20 +161,9 @@ func preRun(cmd *cobra.Command, args []string) {
143161
}
144162
}
145163

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-
157164
// 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"))
160167
os.Exit(errorcodes.ErrBadArgument)
161168
} else {
162169
logrus.SetLevel(lvl)
@@ -177,13 +184,6 @@ func preRun(cmd *cobra.Command, args []string) {
177184
// use the output format to configure the Feedback
178185
feedback.SetFormat(format)
179186

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-
187187
logrus.Info(globals.VersionInfo.Application + "-" + globals.VersionInfo.VersionString)
188188
logrus.Info("Starting root command preparation (`arduino`)")
189189

Diff for: cli/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323

2424
"github.com/arduino/arduino-cli/cli/feedback"
25-
"github.com/arduino/arduino-cli/cli/globals"
2625

2726
"github.com/arduino/arduino-cli/cli/errorcodes"
2827
"github.com/arduino/arduino-cli/cli/instance"
@@ -32,6 +31,7 @@ import (
3231
"github.com/arduino/go-paths-helper"
3332
"github.com/sirupsen/logrus"
3433
"github.com/spf13/cobra"
34+
"github.com/spf13/viper"
3535
)
3636

3737
var (
@@ -107,7 +107,7 @@ func run(cmd *cobra.Command, args []string) {
107107
Quiet: quiet,
108108
VidPid: vidPid,
109109
ExportFile: exportFile,
110-
}, os.Stdout, os.Stderr, globals.LogLevel == "debug")
110+
}, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug")
111111

112112
if err != nil {
113113
feedback.Errorf("Error during build: %v", err)

Diff for: cli/globals/globals.go

-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ var (
3232
VersionInfo = version.NewInfo(filepath.Base(os.Args[0]))
3333
// DefaultIndexURL is the default index url
3434
DefaultIndexURL = "https://downloads.arduino.cc/packages/package_index.json"
35-
// LogLevel is temporarily exported because the compile command will
36-
// forward this information to the underlying legacy package
37-
LogLevel string
3835
)
3936

4037
// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders

Diff for: configuration/defaults.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import (
2222
)
2323

2424
func setDefaults(dataDir, sketchBookDir string) {
25-
// board manager settings
25+
// logging
26+
viper.SetDefault("logging.level", "info")
27+
viper.SetDefault("logging.format", "text")
28+
// board manager
2629
viper.SetDefault("board_manager.additional_urls", []string{})
2730

2831
// arduino directories

0 commit comments

Comments
 (0)