/
log.go
268 lines (233 loc) · 7.49 KB
/
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
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
package log
import (
"fmt"
super "github.com/caibirdme/durian/server"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"path/filepath"
"sync"
"time"
)
const (
defaultAccessLogName = "access.log"
defaultErrLogName = "error.log"
)
var (
globalLogger *zap.Logger
)
// GetLogger returns global logger
// user should only log error info via this logger
func GetLogger() *zap.Logger {
return globalLogger
}
func NewLogger(cfg LogConfig) (EntityWriter, func() error, error) {
err := confirmPath(&cfg)
if err != nil {
return nil, nil, err
}
globalLogger, err = newZapLogger(cfg)
if err != nil {
return nil, nil, err
}
fwriter, err := newFormatWriter(globalLogger, cfg.Format)
if err != nil {
return nil, nil, err
}
return fwriter, globalLogger.Sync, nil
}
func confirmPath(cfg *LogConfig) error {
if filepath.Ext(cfg.AccessPath) == "" {
cfg.AccessPath = filepath.Join(cfg.AccessPath, defaultAccessLogName)
}
dir := filepath.Dir(cfg.AccessPath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
if filepath.Ext(cfg.ErrPath) == "" {
cfg.ErrPath = filepath.Join(cfg.ErrPath, defaultErrLogName)
}
dir = filepath.Dir(cfg.ErrPath)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return nil
}
func newZapLogger(cfg LogConfig) (*zap.Logger, error) {
zapCfg := zap.Config{
Level: zap.NewAtomicLevel(),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: newProductionEncoderConfig(),
OutputPaths: []string{cfg.AccessPath},
ErrorOutputPaths: []string{cfg.ErrPath},
}
return zapCfg.Build()
}
func newProductionEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
type EntityWriter interface {
Write(ctx *fasthttp.RequestCtx)
}
type formatWriter struct {
l *zap.Logger
writers []partialWriter
}
var bufPool = sync.Pool{
New: func() interface{} {
return make([]zapcore.Field, 0, 6)
},
}
func (f *formatWriter) Write(ctx *fasthttp.RequestCtx) {
fields := bufPool.Get().([]zapcore.Field)
fields = fields[:0]
for _, h := range f.writers {
fields = append(fields, h(ctx))
}
f.l.Info("", fields...)
bufPool.Put(fields)
}
func newFormatWriter(l *zap.Logger, format []string) (*formatWriter, error) {
writers := make([]partialWriter, 0, len(format))
for _, val := range format {
h, ok := writerDict[val]
if !ok {
return nil, fmt.Errorf("[log] %s not supported", val)
}
writers = append(writers, h)
}
return &formatWriter{
l: l,
writers: writers,
}, nil
}
// caller's responsibility to ensure entity isn't nil
type partialWriter func(ctx *fasthttp.RequestCtx) zapcore.Field
const (
entryStartTime = "start_time"
entryKeyBytesSent = "bytes_sent"
entryKeyBodyBytesSent = "body_bytes_sent"
entryKeyConnectionRequests = "connection_requests"
entryKeyProcessTime = "process_time"
entryKeyRequestLength = "request_length"
entryKeyStatusCode = "status"
entryKeyUA = "user_agent"
entryKeyRemoteAddr = "remote_addr"
entryKeyRequestURI = "request_uri"
entryKeyQueryString = "query_string"
entryKeyRequestBody = "request_body"
entryKeyRequestHeader = "request_header"
entryKeyMethod = "method"
entryKeyResponseBody = "response_body"
entryKeyResponseHeader = "response_header"
entryReferer = "referer"
entryRequestID = "request_id"
entryKeyHost = "host"
)
var (
writerDict = map[string]partialWriter{
entryKeyHost: hostWriter,
entryRequestID: requestIDWriter,
entryStartTime: startTimeWriter,
entryReferer: refererWriter,
entryKeyBytesSent: bytesSentWriter,
entryKeyBodyBytesSent: bodyBytesSentWriter,
entryKeyConnectionRequests: connectionRequestsWriter,
entryKeyProcessTime: processTimeWriter,
entryKeyRequestLength: requestLengthWriter,
entryKeyStatusCode: statusCodeWriter,
entryKeyUA: userAgentWriter,
entryKeyRemoteAddr: remoteAddrWriter,
entryKeyRequestURI: requestURIWriter,
entryKeyQueryString: queryStringWriter,
entryKeyRequestBody: requestBodyWriter,
entryKeyRequestHeader: requestHeaderWriter,
entryKeyMethod: methodWriter,
entryKeyResponseBody: responseBodyWriter,
entryKeyResponseHeader: responseHeaderWriter,
}
)
func hostWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyHost, ctx.Host())
}
func requestIDWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
headerKey := ctx.UserValue(super.RequestIDHeaderName)
var reqID []byte
if headerKey != nil {
reqID = ctx.Request.Header.Peek(headerKey.(string))
}
if len(reqID) == 0 {
return zap.String(entryRequestID, "-")
} else {
return zap.ByteString(entryRequestID, reqID)
}
}
func refererWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryReferer, ctx.Referer())
}
func startTimeWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.Time(entryStartTime, ctx.Time())
}
func responseHeaderWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyResponseHeader, ctx.Response.Header.Header())
}
func responseBodyWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyResponseBody, ctx.Response.Body())
}
func methodWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyMethod, ctx.Method())
}
func requestHeaderWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyRequestHeader, ctx.Request.Header.Header())
}
func requestBodyWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyRequestBody, ctx.Request.Body())
}
func queryStringWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyQueryString, ctx.Request.URI().QueryString())
}
func requestURIWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyRequestURI, ctx.Request.URI().Path())
}
func remoteAddrWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.String(entryKeyRemoteAddr, ctx.RemoteAddr().String())
}
func userAgentWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.ByteString(entryKeyUA, ctx.Request.Header.UserAgent())
}
func bytesSentWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
count := len(ctx.Response.Header.Header()) + len(ctx.Response.Body())
return zap.Int(entryKeyBytesSent, count)
}
func bodyBytesSentWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.Int(entryKeyBodyBytesSent, len(ctx.Response.Body()))
}
func connectionRequestsWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.Uint64(entryKeyConnectionRequests, ctx.ConnRequestNum())
}
func processTimeWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.Duration(entryKeyProcessTime, time.Since(ctx.Time()))
}
func requestLengthWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
count := len(ctx.Request.Header.Header()) + len(ctx.Request.Body())
return zap.Int(entryKeyRequestLength, count)
}
func statusCodeWriter(ctx *fasthttp.RequestCtx) zapcore.Field {
return zap.Int(entryKeyStatusCode, ctx.Response.StatusCode())
}