forked from gyozatech/noodlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
250 lines (213 loc) · 5.71 KB
/
logger.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
package noodlog
import (
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
)
// logLevel represents the log level flag
var logLevel int = infoLevel
// logWriter is the io.Writer where messages get written
var logWriter io.Writer = os.Stdout
// JSONPrettyPrint represents the pretty printing flag
var JSONPrettyPrint bool = false
// obscureSensitiveDataEnabled represents the sensitive data obscuration flag
var obscureSensitiveDataEnabled bool = false
var logLevels = map[string]int{
traceLabel: traceLevel,
debugLabel: debugLevel,
infoLabel: infoLevel,
warnLabel: warnLevel,
errorLabel: errorLevel,
panicLabel: panicLevel,
fatalLabel: fatalLevel,
}
var sensitiveParams = []string{}
// SetConfigs function allows you to add all the configs at once
func SetConfigs(configs Configs) {
if configs.LogLevel != nil {
LogLevel(*configs.LogLevel)
}
if configs.JSONPrettyPrint != nil {
JSONPrettyPrint = *configs.JSONPrettyPrint
}
if configs.TraceCaller != nil {
traceCallerEnabled = *configs.TraceCaller
}
if configs.SinglePointTracing != nil {
if *(configs.SinglePointTracing) {
EnableSinglePointTracing()
} else {
DisableSinglePointTracing()
}
}
if configs.Colors != nil {
colorEnabled = *configs.Colors
}
if configs.CustomColors != nil {
setCustomColors(*configs.CustomColors)
}
if configs.ObscureSensitiveData != nil {
obscureSensitiveDataEnabled = *configs.ObscureSensitiveData
}
if configs.SensitiveParams != nil {
SetSensitiveParams(configs.SensitiveParams)
}
}
// LogLevel function sets the log level
func LogLevel(level string) {
logLevel = logLevels[level]
if logLevel == 0 {
logLevel = infoLevel
}
}
// LogWriter function sets the new writer
// TODO: if handle is a file, disable color and indentation ?
func LogWriter(w io.Writer) {
logWriter = w
}
// EnableJSONPrettyPrint enables JSON pretty printing
func EnableJSONPrettyPrint() {
JSONPrettyPrint = true
}
// DisableJSONPrettyPrint diables JSON pretty printing
func DisableJSONPrettyPrint() {
JSONPrettyPrint = false
}
// EnableObscureSensitiveData enables sensitive data obscuration from json logs
func EnableObscureSensitiveData(params []string) {
obscureSensitiveDataEnabled = true
SetSensitiveParams(params)
}
// DisableObscureSensitiveData disables sensitive data obscuration from json logs
func DisableObscureSensitiveData() {
obscureSensitiveDataEnabled = false
}
// SetSensitiveParams sets sensitive data obscuration from json logs
func SetSensitiveParams(params []string) {
sensitiveParams = params
}
// Trace function prints a log with trace log level
func Trace(message ...interface{}) {
printLog(traceLabel, message)
}
// Debug function prints a log with debug log level
func Debug(message ...interface{}) {
printLog(debugLabel, message)
}
// Info function prints a log with info log level
func Info(message ...interface{}) {
printLog(infoLabel, message)
}
// Warn function prints a log with warn log level
func Warn(message ...interface{}) {
printLog(warnLabel, message)
}
// Error function prints a log with error log level
func Error(message ...interface{}) {
printLog(errorLabel, message)
}
// Panic function prints a log with panic log level
func Panic(message ...interface{}) {
panic(composeLog(panicLabel, message))
}
// Fatal function prints a log with fatal log level
func Fatal(message ...interface{}) {
printLog(fatalLabel, message)
os.Exit(1)
}
func printLog(label string, message []interface{}) {
if logLevels[label] >= logLevel {
fmt.Fprintf(logWriter, composeLog(label, message))
}
}
func composeLog(level string, message []interface{}) string {
logMsg := record{
Level: level,
Message: composeMessage(message),
Time: strings.Split(time.Now().String(), "m")[0],
}
if traceCallerEnabled {
f, fx := traceCaller()
logMsg.File = &f
logMsg.Function = &fx
}
var jsn []byte
if JSONPrettyPrint {
jsn, _ = json.MarshalIndent(logMsg, "", " ")
} else {
jsn, _ = json.Marshal(logMsg)
}
logRecord := string(jsn)
if colorEnabled {
logRecord = fmt.Sprintf("%s%s%s", colorMap[level], logRecord, colorReset)
}
return logRecord
}
func composeMessage(message []interface{}) interface{} {
switch len(message) {
case 0:
return ""
case 1:
return adaptMessage(message[0])
default:
switch message[0].(type) {
case string:
msg0 := message[0].(string)
if strings.Contains(msg0, "%") {
return fmt.Sprintf(msg0, message[1:]...)
}
}
return stringify(message)
}
}
func stringify(message []interface{}) string {
var b strings.Builder
for _, m := range message {
if m != nil {
fmt.Fprintf(&b, "%v ", m)
}
}
msg := b.String()
return msg[:len(msg)-1]
}
func adaptMessage(message interface{}) interface{} {
switch message.(type) {
case string:
strMsg := message.(string)
if obscureSensitiveDataEnabled && len(sensitiveParams) != 0 {
return strToObj(obscureSensitiveData(strMsg))
}
return strToObj(strMsg)
default:
if obscureSensitiveDataEnabled && len(sensitiveParams) != 0 {
jsn, _ := json.Marshal(message)
strMsg := obscureSensitiveData(string(jsn))
return strToObj(strMsg)
}
}
return message
}
func strToObj(strMsg string) interface{} {
if byteMsg := []byte(strMsg); json.Valid(byteMsg) {
var obj interface{}
_ = json.Unmarshal(byteMsg, &obj)
return obj
}
return strMsg
}
func obscureSensitiveData(jsn string) string {
for _, param := range sensitiveParams {
jsn = obscureParam(jsn, param)
}
return jsn
}
func obscureParam(jsn string, param string) string {
rWithSlash := *regexp.MustCompile(`\\"` + param + `\\":.*?"(.*?)\\"`)
jsn = rWithSlash.ReplaceAllString(jsn, `\"`+param+`\": \"**********\"`)
rWithoutSlash := *regexp.MustCompile(`"` + param + `":.*?"(.*?)"`)
return rWithoutSlash.ReplaceAllString(jsn, `"`+param+`": "**********"`)
}