Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable configuring several log outputs #98

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strings"
"sync"

"go.uber.org/zap"
Expand All @@ -29,6 +30,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
Expand Down Expand Up @@ -263,5 +265,20 @@ func configFromEnv() Config {
cfg.Stderr = false
}

output := os.Getenv(envLoggingOutput)
outputOptions := strings.Split(output, "+")
for _, opt := range outputOptions {
switch opt {
case "stdout":
cfg.Stdout = true
case "stderr":
cfg.Stderr = true
case "file":
if cfg.File == "" {
fmt.Fprint(os.Stderr, "please specify a GOLOG_FILE value to write to")
}
}
}

return cfg
}
85 changes: 85 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -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()
}
}