forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
61 lines (51 loc) · 1.65 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
package log_test
import (
"bytes"
"errors"
"strings"
"testing"
"github.com/rs/zerolog"
"cosmossdk.io/log"
)
func inner() error {
return errors.New("seems we have an error here")
}
type _MockHook string
func (h _MockHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
e.Bool(string(h), true)
}
func TestLoggerOptionHooks(t *testing.T) {
buf := new(bytes.Buffer)
var (
mockHook1 _MockHook = "mock_message1"
mockHook2 _MockHook = "mock_message2"
)
logger := log.NewLogger(buf, log.HooksOption(mockHook1, mockHook2), log.ColorOption(false))
logger.Info("hello world")
if !strings.Contains(buf.String(), "mock_message1=true") {
t.Fatalf("expected mock_message1=true, got: %s", buf.String())
}
if !strings.Contains(buf.String(), "mock_message2=true") {
t.Fatalf("expected mock_message2=true, got: %s", buf.String())
}
buf.Reset()
logger = log.NewLogger(buf, log.HooksOption(), log.ColorOption(false))
logger.Info("hello world")
if !strings.Contains(buf.String(), "hello world") {
t.Fatalf("expected hello world, got: %s", buf.String())
}
}
func TestLoggerOptionStackTrace(t *testing.T) {
buf := new(bytes.Buffer)
logger := log.NewLogger(buf, log.TraceOption(true), log.ColorOption(false))
logger.Error("this log should be displayed", "error", inner())
if strings.Count(buf.String(), "logger_test.go") != 1 {
t.Fatalf("stack trace not found, got: %s", buf.String())
}
buf.Reset()
logger = log.NewLogger(buf, log.TraceOption(false), log.ColorOption(false))
logger.Error("this log should be displayed", "error", inner())
if strings.Count(buf.String(), "logger_test.go") > 0 {
t.Fatalf("stack trace found, got: %s", buf.String())
}
}