diff --git a/app/app.go b/app/app.go index 911bf5e..91d1be0 100644 --- a/app/app.go +++ b/app/app.go @@ -18,7 +18,7 @@ type App struct { } func GetApp(host string, port int, debug bool) *App { - logger.SetupLogger() + logger.SetupLogger(viper.GetString("logger.level")) logger.Logger.Debug(fmt.Sprintf("Starting app with host: %s, port: %d, configFile: %s", host, port, viper.ConfigFileUsed())) app := &App{ Host: host, @@ -41,10 +41,6 @@ func (app *App) setConfigurationDefaults() { func (app *App) loadConfiguration() { viper.AutomaticEnv() - - if err := viper.ReadInConfig(); err == nil { - logger.Logger.Debug(fmt.Sprintf("Using config file: %s", viper.ConfigFileUsed())) - } } func (app *App) configureApplication() { diff --git a/app/app_test.go b/app/app_test.go index 8bd2552..434d3c5 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -2,5 +2,9 @@ package app import "testing" -func DummyTest(*testing.T) { +func GetAppTest(t *testing.T) { + app := GetApp("127.0.0.1", 9999, false) + if app.Port != 9999 || app.Host != "127.0.0.1" { + t.Fail() + } } diff --git a/app/healthcheck_test.go b/app/healthcheck_test.go new file mode 100644 index 0000000..2a51559 --- /dev/null +++ b/app/healthcheck_test.go @@ -0,0 +1,6 @@ +package app + +import "testing" + +func TestHealthCheckHandler(t *testing.T) { +} diff --git a/cmd/root.go b/cmd/root.go index ef61d81..fa7f3c7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,9 +43,13 @@ func initConfig() { viper.SetConfigName(".mqttbot") // name of config file (without extension) viper.AddConfigPath("$HOME") // adding home directory as first search path viper.AutomaticEnv() // read in environment variables that match + viper.SetDefault("logger.level", "DEBUG") // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { + err := viper.ReadInConfig() + if err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) + } else { + fmt.Printf("Error loading config: ", err) } } diff --git a/config/local.yaml b/config/local.yaml index 95ed3e0..eaa6088 100644 --- a/config/local.yaml +++ b/config/local.yaml @@ -32,3 +32,5 @@ redis: port: 6379 password: "" maxPoolSize: 10 +logger: + level: "debug" diff --git a/logger/logger.go b/logger/logger.go index 17cdb5d..f34fd78 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -11,8 +11,14 @@ var format = logging.MustStringFormatter( `%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, ) -func SetupLogger() { +func SetupLogger(logLevel string) { backend := logging.NewLogBackend(os.Stderr, "", 0) backendFormatter := logging.NewBackendFormatter(backend, format) - logging.SetBackend(backendFormatter) + backendLeveled := logging.AddModuleLevel(backendFormatter) + level, err := logging.LogLevel(logLevel) + if err != nil { + panic(err) + } + backendLeveled.SetLevel(level, "") + logging.SetBackend(backendLeveled) }