feat: expose SetLogLevel in cli
Allow user to set a desired log level on startup with `--log-level Level`.
Omitting the flag results in the default INFO
fix: add options, labels as work in progress
@@ -14,6 +14,7 @@ import (
)
var (
+ logLevel string
configFile string
// parsed config
conf config.Config
@@ -22,14 +23,34 @@ var (
RequireCache bool
)
+func getLogLevelFromString(level string) (log.Level, error) {
+ switch level {
+ case "TRACE":
+ return log.TRACE, nil
+ case "DEBUG":
+ return log.DEBUG, nil
+ case "INFO":
+ return log.INFO, nil
+ case "WARN":
+ return log.WARN, nil
+ case "ERROR":
+ return log.ERROR, nil
+ default:
+ return 0, fmt.Errorf("invalid log level use")
+ }
+}
+
func init() {
// root
RootCmd.PersistentFlags().StringVar(&configFile, "config", "config.toml",
"path or http url to a config file, or \"-\" for stdin")
+ RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO",
+ "work in progress - set log level to: TRACE, DEBUG, INFO, WARN or ERROR")
// server
serverCmd.Flags().StringVarP(&serverPort, "port", "p", ":8080", "port to bind tile server to")
serverCmd.Flags().BoolVarP(&serverNoCache, "no-cache", "n", false, "turn off the cache")
+
RootCmd.AddCommand(serverCmd)
// cache seed / purge
cachecmd.Config = &conf
@@ -55,12 +76,18 @@ func rootCmdValidatePersistent(cmd *cobra.Command, args []string) (err error) {
build.Commands = append(build.Commands, cmdName)
return nil
default:
- return initConfig(configFile, requireCache)
+ return initConfig(configFile, requireCache, logLevel)
}
}
-func initConfig(configFile string, cacheRequired bool) (err error) {
- log.Infof("Loading config file: %v", configFile)
+func initConfig(configFile string, cacheRequired bool, logLevel string) (err error) {
+ // set log level before the first log is called
+ level, err := getLogLevelFromString(logLevel)
+ if err != nil {
+ return err
+ }
+ log.SetLogLevel(level)
+
if conf, err = config.Load(configFile); err != nil {
return err
}
