-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
99 lines (87 loc) · 2.64 KB
/
log_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
package golog
import (
"bytes"
"context"
"fmt"
kratoslog "github.com/go-kratos/kratos/v2/log"
"go.opentelemetry.io/otel/sdk/trace"
"io"
"os"
"regexp"
"testing"
)
const (
exampleLog = "[2022-11-15 11:02:00.093] INFO [app:version,traceId,spanId] [uid=uid] 123 [thread=threadName] [callerStack]: message"
exampleLog2 = "[2022-11-15 11:02:00.093] INFO [app:version,,] [uid=uid] 123 [thread=threadName] [callerStack]: message"
)
var (
parseRegex = regexp.MustCompile(`\[[\d|\s|\\.|\-|:]+]\s+(\w+)\s+\[([^,]*),([^,]*),([^]]*)]\s+\[uid=([^]]*)]\s+(\-?\d*)\s+\[thread=([^]]*)]\s+\[([^]]+)]:\s+(.*)`)
)
// parse params:
// level,app, traceId, spanId,uid, pid, threadName, callerStack, message
func TestRegex(t *testing.T) {
if err := matchRegex(exampleLog); err != nil {
t.Fatalf(err.Error())
}
if err := matchRegex(exampleLog2); err != nil {
t.Fatalf(err.Error())
}
}
func matchRegex(logStr string) error {
res := parseRegex.FindStringSubmatch(logStr)
if len(res) != 10 {
return fmt.Errorf("parse log error: %v", res)
}
fmt.Printf("level: %s,\t", res[1])
fmt.Printf("app: %s,\t", res[2])
fmt.Printf("traceId: %s,\t", res[3])
fmt.Printf("spanId: %s,\t", res[4])
fmt.Printf("uid: %s,\t", res[5])
fmt.Printf("pid: %s,\t", res[6])
fmt.Printf("threadName: %s,\t", res[7])
fmt.Printf("callerStack: %s,\t", res[8])
fmt.Printf("message: %s\n", res[9])
return nil
}
func TestLog(t *testing.T) {
var data = make([]byte, 4096)
buf := bytes.NewBuffer(data)
logger := NewFormatStdLogger(buf, WithServerName("test", "0.0.1"), WithFilterLevel(kratoslog.LevelInfo))
ll := kratoslog.NewHelper(logger)
ll.Infof("test log %s", "aaa")
d, _ := io.ReadAll(buf)
if err := matchRegex(string(d)); err != nil {
t.Fatalf("%s, err:%s", string(d), err.Error())
}
ll.Warnf("test warn log %s", "aaa")
d, _ = io.ReadAll(buf)
if err := matchRegex(string(d)); err != nil {
t.Fatalf("%s, err:%s", string(d), err.Error())
}
p := trace.NewTracerProvider()
tt := p.Tracer("aaa")
ctx, sp := tt.Start(context.WithValue(context.Background(), "uid", "-"), "span")
defer sp.End()
ll.WithContext(ctx).Warnf("test trace log %s", "aaa")
d, _ = io.ReadAll(buf)
if err := matchRegex(string(d)); err != nil {
t.Fatalf("%s, err:%s", string(d), err.Error())
}
}
func TestRatioFromEnv(t *testing.T) {
os.Setenv(OTLPExportRatio, "0.7")
ratio := RatioFromEnv()
if ratio != 0.7 {
t.Fatalf("ratio: %f should be 0.7", ratio)
}
os.Setenv(OTLPExportRatio, "0.a")
ratio = RatioFromEnv()
if ratio != 1.0 {
t.Fatalf("ratio: %f should be 1.0", ratio)
}
os.Setenv(OTLPExportRatio, "0.777")
ratio = RatioFromEnv()
if ratio != 0.777 {
t.Fatalf("ratio: %f should be 0.777", ratio)
}
}