-
Notifications
You must be signed in to change notification settings - Fork 3
/
quote.go
64 lines (54 loc) · 1.38 KB
/
quote.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
package logrusKit
import (
"github.com/sirupsen/logrus"
)
// DisableQuote
/*
PS: 输出中存在 换行字符(\n) 的话,只有禁掉双引号才会生效.
*/
func DisableQuote(logger *logrus.Logger) {
if logger == nil {
logger = logrus.StandardLogger()
}
textFormatter, ok := logger.Formatter.(*logrus.TextFormatter)
if ok {
textFormatter.DisableQuote = true
textFormatter.ForceQuote = false
}
}
func EnableQuote(logger *logrus.Logger) {
if logger == nil {
logger = logrus.StandardLogger()
}
textFormatter, ok := logger.Formatter.(*logrus.TextFormatter)
if ok {
textFormatter.DisableQuote = false
textFormatter.ForceQuote = true
}
}
// DisableQuoteTemporarily 临时禁用双引号(")
/*
@param logger (1) 可以为nil(此时将采用logrus.StandardLogger())
(2) 只有当formatter为 *logrus.TextFormatter 类型,DisableQuote才会生效
*/
func DisableQuoteTemporarily(logger *logrus.Logger, callback func(logger *logrus.Logger)) {
if callback == nil {
return
}
if logger == nil {
logger = logrus.StandardLogger()
}
textFormatter, ok := logger.Formatter.(*logrus.TextFormatter)
if ok {
a := textFormatter.DisableQuote
b := textFormatter.ForceQuote
textFormatter.DisableQuote = true
textFormatter.ForceQuote = false
callback(logger)
// restore
textFormatter.DisableQuote = a
textFormatter.ForceQuote = b
} else {
callback(logger)
}
}