forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pattern.go
166 lines (149 loc) · 3.17 KB
/
pattern.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package log
import (
"bytes"
"fmt"
"io"
"path"
"strings"
"sync"
"time"
)
var patternMap = map[string]func(map[string]interface{}) string{
"T": longTime,
"t": shortTime,
"D": longDate,
"d": shortDate,
"L": keyFactory(_level),
"f": keyFactory(_source),
"i": keyFactory(_instanceID),
"e": keyFactory(_deplyEnv),
"z": keyFactory(_zone),
"S": longSource,
"s": shortSource,
"M": message,
}
// newPatternRender new pattern render
func newPatternRender(format string) Render {
p := &pattern{
bufPool: sync.Pool{New: func() interface{} { return &bytes.Buffer{} }},
}
b := make([]byte, 0, len(format))
for i := 0; i < len(format); i++ {
if format[i] != '%' {
b = append(b, format[i])
continue
}
if i+1 >= len(format) {
b = append(b, format[i])
continue
}
f, ok := patternMap[string(format[i+1])]
if !ok {
b = append(b, format[i])
continue
}
if len(b) != 0 {
p.funcs = append(p.funcs, textFactory(string(b)))
b = b[:0]
}
p.funcs = append(p.funcs, f)
i++
}
if len(b) != 0 {
p.funcs = append(p.funcs, textFactory(string(b)))
}
return p
}
type pattern struct {
funcs []func(map[string]interface{}) string
bufPool sync.Pool
}
// Render implemet Formater
func (p *pattern) Render(w io.Writer, d map[string]interface{}) error {
buf := p.bufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
p.bufPool.Put(buf)
}()
for _, f := range p.funcs {
buf.WriteString(f(d))
}
_, err := buf.WriteTo(w)
return err
}
// Render implemet Formater as string
func (p *pattern) RenderString(d map[string]interface{}) string {
// TODO strings.Builder
buf := p.bufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
p.bufPool.Put(buf)
}()
for _, f := range p.funcs {
buf.WriteString(f(d))
}
return buf.String()
}
func textFactory(text string) func(map[string]interface{}) string {
return func(map[string]interface{}) string {
return text
}
}
func keyFactory(key string) func(map[string]interface{}) string {
return func(d map[string]interface{}) string {
if v, ok := d[key]; ok {
if s, ok := v.(string); ok {
return s
}
return fmt.Sprint(v)
}
return ""
}
}
func longSource(d map[string]interface{}) string {
if fn, ok := d[_source].(string); ok {
return fn
}
return "unknown:0"
}
func shortSource(d map[string]interface{}) string {
if fn, ok := d[_source].(string); ok {
return path.Base(fn)
}
return "unknown:0"
}
func longTime(map[string]interface{}) string {
return time.Now().Format("15:04:05.000")
}
func shortTime(map[string]interface{}) string {
return time.Now().Format("15:04")
}
func longDate(map[string]interface{}) string {
return time.Now().Format("2006/01/02")
}
func shortDate(map[string]interface{}) string {
return time.Now().Format("01/02")
}
func isInternalKey(k string) bool {
switch k {
case _level, _levelValue, _time, _source, _instanceID, _appID, _deplyEnv, _zone:
return true
}
return false
}
func message(d map[string]interface{}) string {
var m string
var s []string
for k, v := range d {
if k == _log {
m = fmt.Sprint(v)
continue
}
if isInternalKey(k) {
continue
}
s = append(s, fmt.Sprintf("%s=%v", k, v))
}
s = append(s, m)
return strings.Join(s, " ")
}