-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
304 lines (258 loc) · 8.39 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package golog
import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"time"
"github.com/natefinch/lumberjack"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var SENSITIVE_HEADER = []string{
"Authorization",
"Signature",
"Apikey",
}
var SENSITIVE_ATTR = map[string]bool{
"password": true,
"license": true,
"license_code": true,
"token": true,
"access_token": true,
"refresh_token": true,
"bank_ac_no": true, //bank account number
"id_number": true, //id number, equivalent to KTP in Indonesia
"mobile": true, //mobile number
"npwp": true, //tax number, equivalent to NPWP in Indonesia
"phone": true, //phone number
"card_no": true, //social security number
"basic_salary": true, //basic salary payslip
"brutto": true, //brutto payslip
"employment_deduction": true, //employment deduction payslip
"functional_allowance": true, //functional allowance payslip
"health_allowance": true, //health allowance payslip
"health_deduction": true, //health deduction payslip
"incentive_income_tax_21": true, //incentive income tax payslip
"income_tax_21": true, //income tax payslip
"jht_allowance": true, //jht allowance payslip
"jkk_allowance": true, //jkk allowance payslilp
"jkm_allowance": true, //jkm allowance payslip
"jkn_allowance": true, //jkn allowance payslip
"loan": true, //loan payslip
"other_deduction": true, //other deduction payslip
"position_allowance": true, //position allowance payslip
"skill_allowance": true, //skill allowance payslip
"special_region_allowance": true, //special region allowance payslip
"take_home_pay": true, //take home pay payslip
"total_allowance": true, //total allowance payslip
"total_deduction": true, //total deduction payslip
"total_wages": true, //total wages payslip
}
type Log struct {
logger *zap.Logger
loggerTDR *zap.Logger
}
func NewLogger(conf Config) LoggerInterface {
rotator := &lumberjack.Logger{
Filename: conf.FileLocation + "/system.log",
MaxSize: conf.FileMaxSize, // megabytes
MaxBackups: conf.FileMaxBackup,
MaxAge: conf.FileMaxAge, // days
}
rotatorTDR := &lumberjack.Logger{
Filename: conf.FileLocation + "/tdr.log",
MaxSize: conf.FileMaxSize, // megabytes
MaxBackups: conf.FileMaxBackup,
MaxAge: conf.FileMaxAge, // days
}
encoderConfig := zap.NewDevelopmentEncoderConfig()
if conf.Env == "production" {
encoderConfig = zap.NewProductionEncoderConfig()
}
encoderConfig.TimeKey = "timestamp"
encoderConfig.LevelKey = "logLevel"
encoderConfig.MessageKey = "message"
encoderConfig.StacktraceKey = "stacktrace"
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
core := zapcore.NewCore(
jsonEncoder,
zapcore.AddSync(rotator),
zap.NewAtomicLevelAt(zap.InfoLevel),
)
coreTDR := zapcore.NewCore(
jsonEncoder,
zapcore.AddSync(rotatorTDR),
zap.NewAtomicLevelAt(zap.InfoLevel),
)
if conf.Stdout {
core = zapcore.NewTee(
core,
zapcore.NewCore(
consoleEncoder,
zapcore.AddSync(os.Stdout),
zap.NewAtomicLevelAt(zap.InfoLevel),
),
)
coreTDR = zapcore.NewTee(
coreTDR,
zapcore.NewCore(
consoleEncoder,
zapcore.AddSync(os.Stdout),
zap.NewAtomicLevelAt(zap.InfoLevel),
),
)
}
appVer := conf.AppVer
content, err := os.ReadFile("version.txt")
if err == nil {
c := string(content)
appVer = strings.TrimSuffix(c, "\n")
}
logger := zap.New(core, zap.AddStacktrace(zap.ErrorLevel), zap.AddCallerSkip(2)).With(
zap.String("app", conf.App),
zap.String("appVer", appVer),
zap.String("env", conf.Env),
)
loggerTDR := zap.New(coreTDR, zap.AddCallerSkip(2)).With(
zap.String("app", conf.App),
zap.String("appVer", appVer),
zap.String("env", conf.Env),
)
return &Log{
logger: logger,
loggerTDR: loggerTDR,
}
}
func (l *Log) Debug(ctx context.Context, msg string, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
l.logger.Debug(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) Info(ctx context.Context, msg string, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
l.logger.Info(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) Warn(ctx context.Context, msg string, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
l.logger.Warn(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) Error(ctx context.Context, msg string, err error, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
fields = append(fields, zap.Any("error", toJSON(err)))
l.logger.Error(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) Fatal(ctx context.Context, msg string, err error, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
fields = append(fields, zap.Any("error", toJSON(err)))
l.logger.Fatal(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) Panic(ctx context.Context, msg string, err error, fields ...zap.Field) {
ctxField := populateFieldFromContext(ctx)
fields = append(fields, ctxField...)
fields = append(fields, zap.Any("error", toJSON(err)))
l.logger.Panic(msg, fields...)
defer l.logger.Sync()
}
func (l *Log) TDR(ctx context.Context, log LogModel) {
fields := populateFieldFromContext(ctx)
fields = append(fields, zap.String("correlationID", log.CorrelationID))
fields = append(fields, zap.Any("header", removeAuth(log.Header)))
fields = append(fields, zap.Any("request", toJSON(maskField(log.Request))))
fields = append(fields, zap.String("statusCode", log.StatusCode))
fields = append(fields, zap.Uint64("httpStatus", log.HttpStatus))
fields = append(fields, zap.Any("response", toJSON(maskField(log.Response))))
fields = append(fields, zap.Int64("rt", log.ResponseTime.Milliseconds()))
fields = append(fields, zap.Any("error", toJSON(log.Error)))
fields = append(fields, zap.Any("otherData", toJSON(log.OtherData)))
l.loggerTDR.Info(":", fields...)
defer l.loggerTDR.Sync()
}
func toJSON(object interface{}) interface{} {
if object == nil {
return nil
}
if w, ok := object.(string); ok {
var jsonobj map[string]interface{}
if err := json.Unmarshal([]byte(w), &jsonobj); err != nil {
return w
}
return jsonobj
}
return object
}
func removeAuth(header interface{}) interface{} {
// Fasthttp
if mapHeader, ok := header.(fasthttp.RequestHeader); ok {
for _, val := range SENSITIVE_HEADER {
mapHeader.Del(val)
}
return string(mapHeader.Header())
}
// Http
if mapHeader, ok := header.(http.Header); ok {
for _, val := range SENSITIVE_HEADER {
mapHeader.Del(val)
}
}
return header
}
func maskField(body interface{}) interface{} {
if bodyByte, ok := body.([]byte); ok {
bodyMap := make(map[string]interface{}, 0)
if err := json.Unmarshal(bodyByte, &bodyMap); err != nil {
return string(bodyByte)
}
for key, value := range bodyMap {
switch value.(type) {
case map[string]interface{}:
valueByte, _ := json.Marshal(value)
bodyMap[key] = maskField(valueByte)
default:
if isSensitiveField(key) {
bodyMap[key] = strings.Repeat("*", 5)
} else {
bodyMap[key] = value
}
}
}
return bodyMap
}
return body
}
func isSensitiveField(key string) bool {
if _, ok := SENSITIVE_ATTR[strings.ToLower(key)]; ok {
return true
}
return false
}
func populateFieldFromContext(ctx context.Context) []zap.Field {
fieldFromCtx := make([]zap.Field, 0)
if v, ok := ctx.Value("traceId").(string); ok {
fieldFromCtx = append(fieldFromCtx, zap.String("traceId", v))
}
if v, ok := ctx.Value("srcIP").(string); ok {
fieldFromCtx = append(fieldFromCtx, zap.String("srcIP", v))
}
if v, ok := ctx.Value("port").(string); ok {
fieldFromCtx = append(fieldFromCtx, zap.String("port", v))
}
if v, ok := ctx.Value("path").(string); ok {
fieldFromCtx = append(fieldFromCtx, zap.String("path", v))
}
return fieldFromCtx
}