Skip to content

Control logging via environment variables instead of flags #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@

After installing `arduino-check`, run the command `arduino-check --help` for usage documentation.

Set the `ARDUINO_CHECK_OFFICIAL` environment variable to "true" to run the checks that only apply to official Arduino
projects.
A few additional configuration options only of use for internal/development use of the tool can be set via environment
variables:

- `ARDUINO_CHECK_OFFICIAL` - Set to `"true"` to run the checks that only apply to official Arduino projects.
- `ARDUINO_CHECK_LOG_LEVEL` - Messages with this level and above will be logged.
- Supported values: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`
- `ARDUINO_CHECK_LOG_FORMAT` - The output format for the logs.
- Supported values: `text`, `json`
2 changes: 0 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func Root() *cobra.Command {
rootCommand.PersistentFlags().String("compliance", "specification", "Configure how strict the tool is. Can be {strict|specification|permissive}")
rootCommand.PersistentFlags().String("format", "text", "The output format can be {text|json}.")
rootCommand.PersistentFlags().String("library-manager", "", "Configure the checks for libraries in the Arduino Library Manager index. Can be {submit|update|false}.\nsubmit: Also run additional checks required to pass before a library is accepted for inclusion in the index.\nupdate: Also run additional checks required to pass before new releases of a library already in the index are accepted.\nfalse: Don't run any Library Manager-specific checks.")
rootCommand.PersistentFlags().String("log-format", "text", "The output format for the logs, can be {text|json}.")
rootCommand.PersistentFlags().String("log-level", "panic", "Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic")
rootCommand.PersistentFlags().String("project-type", "all", "Only check projects of the specified type and their subprojects. Can be {sketch|library|all}.")
rootCommand.PersistentFlags().Bool("recursive", true, "Search path recursively for Arduino projects to check. Can be {true|false}.")
rootCommand.PersistentFlags().String("report-file", "", "Save a report on the checks to this file.")
Expand Down
25 changes: 14 additions & 11 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
}
}

logFormatString, _ := flags.GetString("log-format")
logFormat, err := logFormatFromString(logFormatString)
if err != nil {
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
if logFormatString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_FORMAT"); ok {
logFormat, err := logFormatFromString(logFormatString)
if err != nil {
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
}
logrus.SetFormatter(logFormat)
}
logrus.SetFormatter(logFormat)

logLevelString, _ := flags.GetString("log-level")
logLevel, err := logrus.ParseLevel(logLevelString)
if err != nil {
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
if logLevelString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_LEVEL"); ok {
logLevel, err := logrus.ParseLevel(logLevelString)
if err != nil {
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
}
logrus.SetLevel(logLevel)
} else {
logrus.SetLevel(defaultLogLevel)
}
logrus.SetLevel(logLevel)

superprojectTypeFilterString, _ := flags.GetString("project-type")
superprojectTypeFilter, err = projecttype.FromString(superprojectTypeFilterString)
Expand Down Expand Up @@ -117,7 +121,6 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
"output format": OutputFormat(),
"Library Manager submission mode": customCheckModes[checkmode.LibraryManagerSubmission],
"Library Manager update mode": customCheckModes[checkmode.LibraryManagerIndexed],
"log format": logFormatString,
"log level": logrus.GetLevel().String(),
"superproject type filter": SuperprojectTypeFilter(),
"recursive": Recursive(),
Expand Down
25 changes: 18 additions & 7 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/arduino/arduino-check/result/outputformat"
"github.com/arduino/arduino-check/util/test"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -107,16 +108,26 @@ func TestInitializeLibraryManager(t *testing.T) {
}

func TestInitializeLogFormat(t *testing.T) {
flags := test.ConfigurationFlags()
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "foo")
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths), "Invalid format")

flags.Set("log-format", "foo")
assert.Error(t, Initialize(flags, projectPaths))
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "text")
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "text format")

flags.Set("log-format", "text")
assert.Nil(t, Initialize(flags, projectPaths))
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "json")
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "json format")
}

flags.Set("log-format", "json")
assert.Nil(t, Initialize(flags, projectPaths))
func TestInitializeLogLevel(t *testing.T) {
require.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
assert.Equal(t, defaultLogLevel, logrus.GetLevel(), "Default level")

os.Setenv("ARDUINO_CHECK_LOG_LEVEL", "foo")
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths), "Invalid level")

os.Setenv("ARDUINO_CHECK_LOG_LEVEL", "info")
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "Valid level")
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
}

func TestInitializeProjectType(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions configuration/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package configuration
import (
"github.com/arduino/arduino-check/configuration/checkmode"
"github.com/arduino/arduino-check/project/projecttype"
"github.com/sirupsen/logrus"
)

// Default check modes for each superproject type.
Expand Down Expand Up @@ -58,3 +59,5 @@ var defaultCheckModes = map[projecttype.Type]map[checkmode.Type]bool{
checkmode.Official: false,
},
}

var defaultLogLevel = logrus.FatalLevel