-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_redis.go
71 lines (61 loc) · 1.59 KB
/
type_redis.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
package telescope
import (
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"runtime/debug"
"strings"
"time"
)
// Redis @Bean
type Redis struct {
Connection string `json:"connection"`
Command string `json:"command"`
Time string `json:"time"`
Hostname string `json:"hostname"`
}
func (r *Redis) BindType() string {
return "redis"
}
// RedisSplit 切割标识, 这个标识以后的代码才是业务的
var RedisSplit = "github.com/sirupsen/logrus/"
func (r *Redis) Handler(entry *logrus.Entry) (*entries, []tag) {
b := *r
file, line := GetStackCallFile(string(debug.Stack()), RedisSplit)
b.Connection = file + ":" + line
b.Command = entry.Message
return &entries{
Uuid: uuid.NewV4().String(),
BatchId: NewtelescopeHook().TelescopeUUID(),
FamilyHash: nil,
ShouldDisplayOnIndex: 1,
Type: b.BindType(),
Content: ToContent(b),
CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
}, nil
}
// GetStackCallFile 根据关键字的下一行获取调用文件和行号
func GetStackCallFile(stack, split string) (string, string) {
var file, line string
stack = strings.ReplaceAll(stack, "\n\t", "@NT@")
arr := strings.Split(stack, "\n")
status := 0
file = arr[0]
for _, str := range arr {
index := strings.Index(str, split)
if status == 0 {
if index != -1 {
status = 1
}
} else if status == 1 {
if index == -1 {
arr2 := strings.Split(str, "@NT@")
if len(arr2) != 2 {
break
}
file, line = strStackFile(arr2[1])
break
}
}
}
return file, line
}