From ea955b1abca12506c82eb38f4c090ea99f6cf07b Mon Sep 17 00:00:00 2001 From: lanzafame Date: Mon, 22 Jun 2020 12:14:42 +1000 Subject: [PATCH 1/5] enable configuring several log outputs --- setup.go | 46 ++++++++++++++++++++++++++++ setup_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/setup.go b/setup.go index 79ff0ad..026fdae 100644 --- a/setup.go +++ b/setup.go @@ -29,6 +29,7 @@ const ( envLoggingFmt = "GOLOG_LOG_FMT" envLoggingFile = "GOLOG_FILE" // /path/to/file + envLoggingOutput = "GOLOG_OUTPUT" // possible values: stdout|stderr|file combine multiple values with '+' ) type LogFormat int @@ -263,5 +264,50 @@ func configFromEnv() Config { cfg.Stderr = false } + output := os.Getenv(envLoggingOutput) + //TODO: fix lanzafame's super lazy impl... + switch output { + case "stdout": + cfg.Stdout = true + cfg.Stderr = false + cfg.File = "" + case "stderr": + cfg.Stderr = true + cfg.Stdout = false + cfg.File = "" + case "file": + cfg.File = os.Getenv(envLoggingFile) + if cfg.File != "" { + fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + } + cfg.Stderr = false + cfg.Stdout = false + case "stdout+file", "file+stdout": + cfg.File = os.Getenv(envLoggingFile) + if cfg.File != "" { + fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + } + cfg.Stderr = false + cfg.Stdout = true + case "stderr+file", "file+stderr": + cfg.File = os.Getenv(envLoggingFile) + if cfg.File != "" { + fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + } + cfg.Stderr = true + cfg.Stdout = false + case "stdout+stderr", "stderr+stdout": + cfg.File = "" + cfg.Stderr = true + cfg.Stdout = true + case "stdout+stderr+file", "stdout+file+stderr", "stderr+stdout+file", "stderr+file+stdout", "file+stdout+stderr", "file+stderr+stdout": + cfg.File = os.Getenv(envLoggingFile) + if cfg.File != "" { + fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + } + cfg.Stderr = true + cfg.Stdout = true + } + return cfg } diff --git a/setup_test.go b/setup_test.go index 3b0fe19..549e089 100644 --- a/setup_test.go +++ b/setup_test.go @@ -3,6 +3,7 @@ package log import ( "bytes" "io" + "io/ioutil" "os" "strings" "testing" @@ -37,3 +38,87 @@ func TestGetLoggerDefault(t *testing.T) { } } + +func TestLogToFileAndStderr(t *testing.T) { + // setup stderr + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("failed to open pipe: %v", err) + } + + stderr := os.Stderr + os.Stderr = w + defer func() { + os.Stderr = stderr + }() + + // setup file + logfile, err := ioutil.TempFile("", "go-log-test") + if err != nil { + t.Fatal(err) + } + defer os.Remove(logfile.Name()) + + os.Setenv(envLoggingFile, logfile.Name()) + + // set log output env var + os.Setenv(envLoggingOutput, "file+stderr") + + SetupLogging(configFromEnv()) + + log := getLogger("test") + + want := "scooby" + log.Error(want) + w.Close() + + buf := &bytes.Buffer{} + if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(buf.String(), want) { + t.Errorf("got %q, wanted it to contain log output", buf.String()) + } + + content, err := ioutil.ReadFile(logfile.Name()) + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(string(content), want) { + t.Logf("want: '%s', got: '%s'", want, string(content)) + t.Fail() + } +} + +func TestLogToFile(t *testing.T) { + // get tmp log file + logfile, err := ioutil.TempFile("", "go-log-test") + if err != nil { + t.Fatal(err) + } + defer os.Remove(logfile.Name()) + + // set the go-log file env var + os.Setenv(envLoggingFile, logfile.Name()) + + SetupLogging(configFromEnv()) + + log := getLogger("test") + + // write log to file + want := "grokgrokgrok" + log.Error(want) + + // read log file and check contents + content, err := ioutil.ReadFile(logfile.Name()) + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(string(content), want) { + t.Logf("want: '%s', got: '%s'", want, string(content)) + t.Fail() + } +} \ No newline at end of file From cbd84aad49417b0603cc9508eb58e4f742e507ca Mon Sep 17 00:00:00 2001 From: lanzafame Date: Thu, 25 Jun 2020 11:39:46 +1000 Subject: [PATCH 2/5] simplify option parsing --- setup.go | 53 +++++++++++++---------------------------------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/setup.go b/setup.go index 026fdae..5efa736 100644 --- a/setup.go +++ b/setup.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "strings" "sync" "go.uber.org/zap" @@ -265,48 +266,20 @@ func configFromEnv() Config { } output := os.Getenv(envLoggingOutput) + outputOptions := strings.Split(output, "+") //TODO: fix lanzafame's super lazy impl... - switch output { - case "stdout": - cfg.Stdout = true - cfg.Stderr = false - cfg.File = "" - case "stderr": - cfg.Stderr = true - cfg.Stdout = false - cfg.File = "" - case "file": - cfg.File = os.Getenv(envLoggingFile) - if cfg.File != "" { - fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") - } - cfg.Stderr = false - cfg.Stdout = false - case "stdout+file", "file+stdout": - cfg.File = os.Getenv(envLoggingFile) - if cfg.File != "" { - fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") - } - cfg.Stderr = false - cfg.Stdout = true - case "stderr+file", "file+stderr": - cfg.File = os.Getenv(envLoggingFile) - if cfg.File != "" { - fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") - } - cfg.Stderr = true - cfg.Stdout = false - case "stdout+stderr", "stderr+stdout": - cfg.File = "" - cfg.Stderr = true - cfg.Stdout = true - case "stdout+stderr+file", "stdout+file+stderr", "stderr+stdout+file", "stderr+file+stdout", "file+stdout+stderr", "file+stderr+stdout": - cfg.File = os.Getenv(envLoggingFile) - if cfg.File != "" { - fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + for _, opt := range outputOptions { + switch opt { + case "stdout": + cfg.Stdout = true + case "stderr": + cfg.Stderr = true + case "file": + cfg.File = os.Getenv(envLoggingFile) + if cfg.File != "" { + fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") + } } - cfg.Stderr = true - cfg.Stdout = true } return cfg From c931948652d4dd708b7d03359d384aebdd350fcc Mon Sep 17 00:00:00 2001 From: lanzafame Date: Thu, 25 Jun 2020 11:40:10 +1000 Subject: [PATCH 3/5] remove TODO comment --- setup.go | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.go b/setup.go index 5efa736..166a112 100644 --- a/setup.go +++ b/setup.go @@ -267,7 +267,6 @@ func configFromEnv() Config { output := os.Getenv(envLoggingOutput) outputOptions := strings.Split(output, "+") - //TODO: fix lanzafame's super lazy impl... for _, opt := range outputOptions { switch opt { case "stdout": From 853d8bdd5dd8a2fe580097b0bd74796a8b3bd925 Mon Sep 17 00:00:00 2001 From: lanzafame Date: Thu, 25 Jun 2020 11:55:59 +1000 Subject: [PATCH 4/5] remove unnecessary variable assignment --- setup.go | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.go b/setup.go index 166a112..a3221f4 100644 --- a/setup.go +++ b/setup.go @@ -274,7 +274,6 @@ func configFromEnv() Config { case "stderr": cfg.Stderr = true case "file": - cfg.File = os.Getenv(envLoggingFile) if cfg.File != "" { fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") } From 6ec35df0bfd61a39ec3bab684ee702637d14130b Mon Sep 17 00:00:00 2001 From: lanzafame Date: Thu, 25 Jun 2020 11:58:51 +1000 Subject: [PATCH 5/5] fix boolean statement --- setup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.go b/setup.go index a3221f4..801e5b3 100644 --- a/setup.go +++ b/setup.go @@ -274,7 +274,7 @@ func configFromEnv() Config { case "stderr": cfg.Stderr = true case "file": - if cfg.File != "" { + if cfg.File == "" { fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to") } }