forked from CardInfoLink/log
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstandard.go
271 lines (222 loc) · 5.95 KB
/
standard.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package log
import (
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"text/template"
"time"
)
type record struct {
Date, Time string
Tag string
Level string
File string
Line int
Message string
Stack []byte
}
// Standard 日志输出基本实现
type Standard struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
out io.Writer // destination for output
format string // log format
pattern string // log template
tpl *template.Template
prefixLen int
dateFmt string
timeFmt string
}
// NewStandard 返回标准实现
func NewStandard(out io.Writer, format string) *Standard {
std := &Standard{out: out}
std.ChangeFormat(format)
return std
}
// ChangeWriter 改变输出流
func (s *Standard) ChangeWriter(w io.Writer) {
s.mu.Lock()
s.out = w
s.mu.Unlock()
}
// ChangeFormat 改变日志格式
func (s *Standard) ChangeFormat(format string) {
s.mu.Lock()
defer s.mu.Unlock()
// println(format)
s.format = format
s.pattern = format
s.prefixLen = calculatePrefixLen(format, 5)
// 顺序最好不要变,从最长的开始匹配
s.pattern = strings.Replace(s.pattern, PathToken, "{{ .File }}", -1)
s.pattern = strings.Replace(s.pattern, PackageToken, "{{ .File }}", -1)
s.pattern = strings.Replace(s.pattern, ProjectToken, "{{ .File }}", -1)
s.pattern = strings.Replace(s.pattern, FileToken, "{{ .File }}", -1)
s.pattern = strings.Replace(s.pattern, TagToken, "{{ .Tag }}", -1)
s.pattern = strings.Replace(s.pattern, LevelToken, "{{ .Level }}", -1)
s.pattern = strings.Replace(s.pattern, strconv.Itoa(LineToken), "{{ .Line }}", -1)
s.pattern = strings.Replace(s.pattern, MessageToken, "{{ .Message }}", -1)
// println(s.dateFmt, s.timeFmt)
// 提取出日期和时间的格式化模式字符串
s.dateFmt, s.timeFmt = extactDateTimeFormat(format)
if s.dateFmt != "" {
s.pattern = strings.Replace(s.pattern, s.dateFmt, "{{ .Date }}", -1)
}
if s.timeFmt != "" {
s.pattern = strings.Replace(s.pattern, s.timeFmt, "{{ .Time }}", -1)
}
s.tpl = template.Must(template.New("record").Parse(s.pattern))
}
// Tprintf 打印日志
func (s *Standard) Tprintf(v, l Level, tag string, format string, m ...interface{}) {
if v > l {
return
}
if tag == "" {
tag = "-"
}
r := record{
Level: l.String(),
Tag: tag,
}
if format == "" {
r.Message = fmt.Sprint(m...)
} else {
r.Message = fmt.Sprintf(format, m...)
}
r.Message = strings.TrimRight(r.Message, "\n")
if s.dateFmt != "" {
now := time.Now() // get this early.
r.Date = now.Format(s.dateFmt)
if s.timeFmt != "" {
r.Time = now.Format(s.timeFmt)
}
}
if s.prefixLen > -1 {
var ok bool
_, r.File, r.Line, ok = runtime.Caller(2) // expensive
if ok && s.prefixLen < len(r.File) {
r.File = r.File[s.prefixLen:]
} else {
r.File = "???"
}
}
var buf []byte
if l == StackLevel {
buf = make([]byte, 1024*1024)
n := runtime.Stack(buf, true)
buf = buf[:n]
}
s.mu.Lock()
defer func() {
s.mu.Unlock()
if l == PanicLevel {
panic(m)
}
if l == FatalLevel {
os.Exit(-1)
}
}()
s.tpl.Execute(s.out, r)
s.out.Write([]byte("\n"))
if l == StackLevel {
s.out.Write(buf)
}
}
func extactDateTimeFormat(format string) (dateFmt, timeFmt string) {
// 算法:
// 找出两个字符串不同的部分,
// 如果有两处不同,一个是日期模式,一个是时间模式,
// 如果只有一个,那么只有日期或者只有时间,无关紧要,
// 如果都相同,那么日志里没有时间,
// 如果有三处以上不同,说明格式配置错误
t, _ := time.ParseInLocation("2006-1-2 3:4:5.000000000", "1991-2-1 1:1:1.111111111", time.Local)
contrast := t.Format(format)
// println(format)
// println(contrast)
idxs := [10]int{}
start := -1
for i, l, same := 0, len(format), true; i < l; i++ {
if start > 4 {
panic(fmt.Sprintf("format string error at `%s`", format[i-1:]))
}
// fmt.Printf("%c %c %d %d\n", format[i], contrast[i], idxs, start)
if format[i] != contrast[i] {
if same {
start++
// 如果之前都是相同的,这个开始不同,那么这个就是起始位置
idxs[start] = i
same = false
// println(i, diff, start, idxs[start])
start++
}
idxs[start] = i + 1 // 下一个有可能是结束位置
continue
}
// 如果是 空格、-、:、. ,那么它不一定是结束位置
if format[i] == '-' || format[i] == ' ' || format[i] == ':' || format[i] == '.' {
// 如果这些字符后面是 0(如 2006-01-02),跳过 0
if i+1 < l && format[i+1] == '0' && contrast[i+1] == '0' {
i++
}
continue
}
same = true
}
if start != -1 && start != 1 && start != 3 {
// 正常情况是不可能到这里的,如果到这里,说明算法写错了
panic(fmt.Sprintf("parse error %d", start))
} else {
if start > 0 {
dateFmt = format[idxs[0]:idxs[1]]
if start == 3 {
timeFmt = format[idxs[2]:idxs[3]]
}
}
}
return dateFmt, timeFmt
}
func calculatePrefixLen(format string, skip int) int {
// 格式中不包含文件路径
if !strings.Contains(format, "main.go") {
return -1
}
_, file, _, _ := runtime.Caller(skip)
// file with absolute path
if strings.Contains(format, PathToken) {
return 0
}
// file with package name
if strings.Contains(format, PackageToken) {
return strings.Index(file, "/src/") + 5
}
// file with project path
if strings.Contains(format, ProjectToken) {
// remove /<GOPATH>/src/
prefixLen := strings.Index(file, "/src/") + 5
file = file[prefixLen:]
// remove github.com/
if strings.HasPrefix(file, "github.com/") {
prefixLen += 11
file = file[11:]
// remove github user or org name
if i := strings.Index(file, "/"); i >= 0 {
prefixLen += i + 1
file = file[i+1:]
// remove project name
if i := strings.Index(file, "/"); i >= 0 {
prefixLen += i + 1
}
}
}
return prefixLen
}
// file only
if strings.Contains(format, FileToken) {
return strings.LastIndex(file, "/") + 1
}
return -1
}