Skip to content

Commit

Permalink
Allow to define logger output from env
Browse files Browse the repository at this point in the history
  • Loading branch information
airenas committed Feb 2, 2021
1 parent 8f557a3 commit a8e6369
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/goapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func TestEnvBeatsSubConfigNested(t *testing.T) {
assert.Equal(t, "xxxx", Sub(Config, "message").GetString("server.url"))
}


// the test fails as where is no option to get current env prefix from config
// func TestEnvBeatsSeveralSubConfigNested(t *testing.T) {
// os.Setenv("MESSAGE_SERVER_URL", "xxxx")
Expand Down Expand Up @@ -91,6 +90,21 @@ func TestLoggerLevelInitFromEnv(t *testing.T) {
assert.Equal(t, "trace", Log.GetLevel().String())
}

func TestLoggerOutFromEnv(t *testing.T) {
initDefaultLevel()
assert.Equal(t, os.Stdout, Log.Out)

os.Setenv("LOGGER_OUT_NAME", "stderr")
initAppFromTempFile(t, "logger:\n level: info\n")

assert.Equal(t, os.Stderr, Log.Out)

os.Setenv("LOGGER_OUT_NAME", "stdout")
initAppFromTempFile(t, "logger:\n level: info\n")

assert.Equal(t, os.Stdout, Log.Out)
}

func TestStartWitFlags(t *testing.T) {
f, err := ioutil.TempFile("", "test.*.yml")
assert.Nil(t, err)
Expand Down Expand Up @@ -123,4 +137,5 @@ func initAppFromTempFile(t *testing.T, data string) {

func initDefaultLevel() {
Log.SetLevel(logrus.ErrorLevel)
Log.Out = os.Stdout
}
4 changes: 4 additions & 0 deletions pkg/goapp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func initLogFromEnv(c *logrus_mate.LoggerConfig) {
if ll != "" {
c.Level = ll
}
out := Config.GetString("logger.out.name")
if out != "" {
c.Out.Name = out
}
}

func initDefaultLogConfig() {
Expand Down

0 comments on commit a8e6369

Please sign in to comment.