Skip to content

Commit

Permalink
Merge pull request #98 from ipfs/fix/env-var-config
Browse files Browse the repository at this point in the history
enable configuring several log outputs
  • Loading branch information
Kubuxu authored Jun 26, 2020
2 parents f8d20c3 + 6ec35df commit 0016c0b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
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()
}
}

0 comments on commit 0016c0b

Please sign in to comment.