Skip to content

Commit

Permalink
add logger level variable
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Jun 17, 2016
1 parent 3d4a6ca commit a738cd9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
6 changes: 1 addition & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
6 changes: 5 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
6 changes: 6 additions & 0 deletions app/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

import "testing"

func TestHealthCheckHandler(t *testing.T) {
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ redis:
port: 6379
password: ""
maxPoolSize: 10
logger:
level: "debug"
10 changes: 8 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a738cd9

Please sign in to comment.