-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller_test.go
122 lines (111 loc) · 2.83 KB
/
caller_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package olog
import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
)
func TestLogCaller(t *testing.T) {
buf := bytes.NewBuffer(nil)
SetWriter(NewWriter(buf))
SetEncode(JSON)
SetLevel(TRACE)
Log(Record{Level: DEBUG, MsgOrFormat: "hello %s", MsgArgs: []any{"world"}})
Error("hello")
Errorf("hello %s", "world")
Errorw("hello")
Warn("hello")
Warnf("hello %s", "world")
Warnw("hello")
Notice("hello")
Noticef("hello %s", "world")
Noticew("hello")
Info("hello")
Infof("hello %s", "world")
Infow("hello")
Debug("hello")
Debugf("hello %s", "world")
Debugw("hello")
Trace("hello")
Tracef("hello %s", "world")
Tracew("hello")
err := validCaller(buf, "olog/caller_test.go", 17)
if err != nil {
t.Error(err)
}
}
func TestLoggerCaller(t *testing.T) {
buf := bytes.NewBuffer(nil)
logger := NewLogger(WithLoggerWriter(NewWriter(buf)))
logger.Log(Record{Level: DEBUG, MsgOrFormat: "hello"})
logger.Error("hello")
logger.Errorf("hello %s", "world")
logger.Errorw("hello")
logger.Warn("hello")
logger.Warnf("hello %s", "world")
logger.Warnw("hello")
logger.Notice("hello")
logger.Noticef("hello %s", "world")
logger.Noticew("hello")
logger.Info("hello")
logger.Infof("hello %s", "world")
logger.Infow("hello")
logger.Debug("hello")
logger.Debugf("hello %s", "world")
logger.Debugw("hello")
logger.Trace("hello")
logger.Tracef("hello %s", "world")
logger.Tracew("hello")
err := validCaller(buf, "olog/caller_test.go", 47)
if err != nil {
t.Error(err)
}
}
func TestContextLoggerCaller(t *testing.T) {
buf := bytes.NewBuffer(nil)
logger := NewLogger(WithLoggerWriter(NewWriter(buf)))
logger = WithContext(logger, context.Background())
logger = WithContext(logger, context.WithValue(context.Background(), "name", "bob"))
logger.Log(Record{Level: DEBUG, MsgOrFormat: "hello"})
logger.Error("hello")
logger.Errorf("hello %s", "world")
logger.Errorw("hello")
logger.Warn("hello")
logger.Warnf("hello %s", "world")
logger.Warnw("hello")
logger.Notice("hello")
logger.Noticef("hello %s", "world")
logger.Noticew("hello")
logger.Info("hello")
logger.Infof("hello %s", "world")
logger.Infow("hello")
logger.Debug("hello")
logger.Debugf("hello %s", "world")
logger.Debugw("hello")
logger.Trace("hello")
logger.Tracef("hello %s", "world")
logger.Tracew("hello")
err := validCaller(buf, "olog/caller_test.go", 79)
if err != nil {
t.Error(err)
}
}
func validCaller(buf *bytes.Buffer, file string, startLine int) error {
for {
line, _ := buf.ReadBytes('\n')
if len(line) == 0 {
break
}
m := make(map[string]any)
if err := json.Unmarshal(line, &m); err != nil {
return fmt.Errorf("unmarshal data %s err: %w", string(line), err)
}
want := fmt.Sprintf("%s:%d", file, startLine)
if m[fieldCaller] != want {
return fmt.Errorf("caller is %s, want %s", m[fieldCaller], want)
}
startLine++
}
return nil
}