forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
98 lines (89 loc) · 2.63 KB
/
logger_test.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package log_test
import (
"bytes"
"flag"
"regexp"
"testing"
"github.com/sirupsen/logrus"
"github.com/compose/transporter/log"
)
var (
loggerTests = []struct {
logLines func()
logLevel logrus.Level
re *regexp.Regexp
}{
{
func() {
log.Base().Debugln("This debug-level line should not show up in the output.")
log.Base().Infof("This %s-level line should show up in the output.", "info")
},
logrus.InfoLevel,
regexp.MustCompile(`^time=".*" level=info msg="This info-level line should show up in the output."\n$`),
},
{
func() {
log.Debugf("This %s-level line should show up in the output.", "debug")
},
logrus.DebugLevel,
regexp.MustCompile(`^time=".*" level=debug msg="This debug-level line should show up in the output."\n$`),
},
{
func() {
log.Debugln("This debug-level line should not show up in the output.")
log.Infof("This %s-level line should not show up in the output.", "info")
log.Errorf("This %s-level line should show up in the output.", "error")
},
logrus.ErrorLevel,
regexp.MustCompile(`^time=".*" level=error msg="This error-level line should show up in the output."\n$`),
},
{
func() {
log.Errorln("This error-level line should show up in the output.")
log.Infoln("This info-level line should not show up in the output.")
},
logrus.ErrorLevel,
regexp.MustCompile(`^time=".*" level=error msg="This error-level line should show up in the output."\n$`),
},
{
func() {
log.With("key", "value").Infoln("This info-level line should show up in the output.")
},
logrus.InfoLevel,
regexp.MustCompile(`^time=".*" level=info msg="This info-level line should show up in the output." key=value\n$`),
},
{
func() {
log.Base().Output(0, "This info-level line should show up in the output.")
},
logrus.InfoLevel,
regexp.MustCompile(`^time=".*" level=info msg="This info-level line should show up in the output."\n$`),
},
}
)
func TestFileLineLogging(t *testing.T) {
for _, lt := range loggerTests {
var buf bytes.Buffer
log.Orig().Out = &buf
log.Orig().Level = lt.logLevel
log.Orig().Formatter = &logrus.TextFormatter{
DisableColors: true,
}
lt.logLines()
if !lt.re.Match(buf.Bytes()) {
t.Fatalf("%q did not match expected regex %q", buf.String(), lt.re.String())
}
}
}
func TestCommandLineFlag(t *testing.T) {
if err := flag.Set("log.level", "error"); err != nil {
t.Fatalf("unexpected flag.Set error, %s", err)
}
flag.Parse()
}
func TestCommandLineFlagErr(t *testing.T) {
if err := flag.Set("log.level", "erro"); err == nil {
t.Fatal("expected flag.Set error, none received")
}
flag.Parse()
}