-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard_log.go
183 lines (153 loc) · 6.4 KB
/
standard_log.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
package log
import (
"context"
"github.com/kodekoding/phastos/go/log/logger"
xid "github.com/rs/xid"
)
// RFC3339Milli We agree that we want a millisecond precisstion, sadly in golang it's not there. https://github.com/golang/go/issues/13291
const (
RFC3339Milli = "2006-01-02T15:04:05.999Z07:00"
)
// SetStdLog will be the entry point for tdk logging library to make sure all Tokopedia services have the same log structures.
// The general specification will be JSON, request id, context id, metadata, error and message.
// For more specifications please go to https://tokopedia.atlassian.net/wiki/spaces/EN/pages/694817819/Logging+Format+Standardization
func SetStdLog(config *Config) error {
var (
newLogger logger.Logger
newDebugLogger logger.Logger
err error
loggerConfig = logger.Config{Level: logger.InfoLevel}
debugLoggerConfig = loggerConfig
)
if config != nil {
loggerConfig = logger.Config{
Level: logger.StringToLevel(config.Level),
LogFile: config.LogFile, // Default will goes to os.Stderr
TimeFormat: RFC3339Milli,
Caller: true,
AppName: config.AppName,
UseJSON: true,
StdLog: true,
UseColor: isDev,
CallerSkip: config.CallerSkip,
}
debugLoggerConfig = logger.Config{
Level: logger.StringToLevel(config.Level),
LogFile: config.DebugFile, // Default will goes to os.Stderr
TimeFormat: RFC3339Milli,
Caller: true,
AppName: config.AppName,
UseJSON: true,
StdLog: true,
UseColor: isDev,
CallerSkip: config.CallerSkip,
Debug: true,
}
}
newLogger, err = NewLogger(Zerolog, &loggerConfig)
if err != nil {
return err
}
newDebugLogger, err = NewLogger(Zerolog, &debugLoggerConfig)
if err != nil {
return err
}
// extra check because it is very difficult to debug if the log itself causes the panic
if newLogger != nil && debugLogger != nil {
traceLogger = newDebugLogger
debugLogger = newDebugLogger
infoLogger = newDebugLogger
warnLogger = newLogger
errorLogger = newLogger
fatalLogger = newLogger
}
return nil
}
// StdTrace print trace level log with standardized parameters.
func StdTrace(ctx context.Context, metadata interface{}, err error, message string) {
traceLogger.StdTrace(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdTracef print trace level log with standardized parameters with formatter.
func StdTracef(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
traceLogger.StdTracef(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
// StdDebug print trace level log with standardized parameters.
func StdDebug(ctx context.Context, metadata interface{}, err error, message string) {
debugLogger.StdDebug(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdDebugf print trace level log with standardized parameters with formatter.
func StdDebugf(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
debugLogger.StdDebugf(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
// StdInfo print trace level log with standardized parameters.
func StdInfo(ctx context.Context, metadata interface{}, err error, message string) {
infoLogger.StdInfo(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdInfof print trace level log with standardized parameters with formatter.
func StdInfof(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
infoLogger.StdInfof(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
// StdWarn print trace level log with standardized parameters.
func StdWarn(ctx context.Context, metadata interface{}, err error, message string) {
warnLogger.StdWarn(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdWarnf print trace level log with standardized parameters with formatter.
func StdWarnf(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
warnLogger.StdWarnf(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
// StdError print trace level log with standardized parameters.
func StdError(ctx context.Context, metadata interface{}, err error, message string) {
errorLogger.StdError(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdErrorf print trace level log with standardized parameters with formatter.
func StdErrorf(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
errorLogger.StdErrorf(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
// StdFatal print trace level log with standardized parameters.
func StdFatal(ctx context.Context, metadata interface{}, err error, message string) {
fatalLogger.StdFatal(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, message)
}
// StdFatalf print trace level log with standardized parameters with formatter.
func StdFatalf(ctx context.Context, metadata interface{}, err error, format string, args ...interface{}) {
fatalLogger.StdFatalf(GetCtxRequestID(ctx), GetCtxID(ctx), err, metadata, format, args...)
}
type contextKey string
const (
contextKeyRequestID = contextKey("tkpd-log-request-id")
contextKeyID = contextKey("tkpd-log-context-id")
)
// InitLogContext will initialize context with some informations
// Currently it will inject request id using xid
// Ideally InitLogContext will be wrap around middleware, either grpc or http
// For Request ID will currently set it with xid if empty. Can be explored with trace id
func InitLogContext(ctx context.Context) context.Context {
// Check if request id is in context
if GetCtxRequestID(ctx) == "" {
return SetCtxRequestID(ctx, xid.New().String())
}
return ctx
}
// SetCtxRequestID set context with request id
func SetCtxRequestID(ctx context.Context, uuid string) context.Context {
return context.WithValue(ctx, contextKeyRequestID, uuid)
}
// SetCtxID set context with context it
func SetCtxID(ctx context.Context, contextID string) context.Context {
return context.WithValue(ctx, contextKeyID, contextID)
}
// GetCtxRequestID get request id from context
func GetCtxRequestID(ctx context.Context) string {
id, ok := ctx.Value(contextKeyRequestID).(string)
if !ok {
return ""
}
return id
}
// GetCtxID get request id from context
func GetCtxID(ctx context.Context) string {
id, ok := ctx.Value(contextKeyID).(string)
if !ok {
return ""
}
return id
}