1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package cmd
import (
"bytes"
"log"
"os"
"strings"
"testing"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)
func TestLogFormat(t *testing.T) {
os.Unsetenv("GOMPLATE_LOG_FORMAT")
defer os.Unsetenv("GOMPLATE_LOG_FORMAT")
assert.Equal(t, "json", logFormat(nil))
// os.Stdout isn't a terminal when this runs as a unit test...
assert.Equal(t, "json", logFormat(os.Stdout))
os.Setenv("GOMPLATE_LOG_FORMAT", "simple")
assert.Equal(t, "simple", logFormat(os.Stdout))
assert.Equal(t, "simple", logFormat(&bytes.Buffer{}))
}
func TestCreateLogger(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
buf := &bytes.Buffer{}
// avoid timestamps
zlog.Logger = zerolog.New(buf)
l := createLogger("", buf)
l.Debug().Msg("this won't show up")
l.Warn().Msg("hello world")
actual := strings.TrimSpace(buf.String())
assert.Equal(t, `{"level":"warn","msg":"hello world"}`, actual)
buf.Reset()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
l = createLogger("simple", buf)
l.Debug().Str("field", "a value").Msg("this will show up")
log.Println("hello world")
actual = strings.TrimSpace(buf.String())
assert.Equal(t, "this will show up field=\"a value\"\nhello world stdlog=true", actual)
buf.Reset()
l = createLogger("console", buf)
l.Debug().Msg("hello")
log.Println("world")
actual = strings.TrimSpace(buf.String())
assert.Equal(t, "<nil> DBG hello\n<nil> world stdlog=true", actual)
buf.Reset()
l = createLogger("logfmt", buf)
l.Info().Str("field", "a value").Int("num", 84).Msg("hello\"")
actual = strings.TrimSpace(buf.String())
assert.Equal(t, "level=info msg=\"hello\\\"\" field=\"a value\" num=84", actual)
}
|