-
Notifications
You must be signed in to change notification settings - Fork 17
/
logging.go
182 lines (160 loc) · 3.46 KB
/
logging.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
package logging
import "os"
import "fmt"
import "strings"
import "time"
import "bytes"
import "runtime/debug"
import l "log"
type LogLevel int
// public interface
const (
Silent LogLevel = iota
Fatal
Error
Warn
Info
Verbose
Timing
Debug
Trace
)
var (
LogTimeFormat = "2006-01-02T15:04:05.000-07:00"
)
type Logger interface {
// Warnings, logged by default.
Warnf(format string, v ...interface{})
// Errors, logged by default.
Errorf(format string, v ...interface{})
// Fatal errors. Will not terminate execution.
Fatalf(format string, v ...interface{})
// Informational messages.
Infof(format string, v ...interface{})
// Verbose messages like request logging
Verbosef(format string, v ...interface{})
// Get stack trace
StackTrace() string
// Debugging messages
Debugf(format string, v ...interface{})
// Program execution - most verbose
Tracef(format string, v ...interface{})
}
// implementation
func (t LogLevel) String() string {
switch t {
case Silent:
return "Silent"
case Fatal:
return "Fatal"
case Error:
return "Error"
case Warn:
return "Warn"
case Info:
return "Info"
case Verbose:
return "Verbose"
case Timing:
return "Timing"
case Debug:
return "Debug"
case Trace:
return "Trace"
default:
return "Info"
}
}
func Level(s string) LogLevel {
switch strings.ToUpper(s) {
case "SILENT":
return Silent
case "FATAL":
return Fatal
case "ERROR":
return Error
case "WARN":
return Warn
case "INFO":
return Info
case "VERBOSE":
return Verbose
case "TIMING":
return Timing
case "DEBUG":
return Debug
case "TRACE":
return Trace
default:
return Info
}
}
var baselevel LogLevel
var target *l.Logger
var noredact bool
func init() {
target = l.New(os.Stdout, "", 0)
baselevel = Info
noredact = os.Getenv("CB_EVENTING_NOREDACT") == "true"
}
func printf(at LogLevel, format string, v ...interface{}) {
if baselevel >= at {
format := RedactFormat(format)
ts := time.Now().Format(LogTimeFormat)
msg := fmt.Sprintf(ts+" ["+at.String()+"] "+format, v...)
if !strings.Contains(msg, "[gocb] Threshold Log:") { // sigh
target.Print(msg)
}
}
}
func RedactFormat(format string) string {
if noredact {
format = strings.Replace(format, "%ru", "%v", -1)
} else {
format = strings.Replace(format, "%ru", "<ud>%v</ud>", -1)
}
format = strings.Replace(format, "%rm", "%v", -1) // not implemented
format = strings.Replace(format, "%rs", "%v", -1) // not implemented
return format
}
func Warnf(format string, v ...interface{}) {
printf(Warn, format, v...)
}
func Errorf(format string, v ...interface{}) {
printf(Error, format, v...)
}
func Fatalf(format string, v ...interface{}) {
printf(Fatal, format, v...)
}
func Infof(format string, v ...interface{}) {
printf(Info, format, v...)
}
func Verbosef(format string, v ...interface{}) {
printf(Verbose, format, v...)
}
func Debugf(format string, v ...interface{}) {
printf(Debug, format, v...)
}
func Tracef(format string, v ...interface{}) {
printf(Trace, format, v...)
}
func IsEnabled(at LogLevel) bool {
return baselevel >= at
}
func SetLogLevel(to LogLevel) {
baselevel = to
}
func StackTrace() string {
var buf bytes.Buffer
lines := strings.Split(string(debug.Stack()), "\n")
for _, call := range lines[2:] {
buf.WriteString(fmt.Sprintf("%s\n", call))
}
return buf.String()
}
// log redaction related
func TagUD(arg interface{}) interface{} {
var udtag_begin = "<ud>"
var udtag_end = "</ud>"
return fmt.Sprintf("%s(%v)%s", udtag_begin, arg, udtag_end)
}