-
Notifications
You must be signed in to change notification settings - Fork 12
/
logging.go
34 lines (28 loc) · 928 Bytes
/
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
package interceptor
import (
"context"
"github.com/gojek/fiber"
"go.uber.org/zap"
)
// NewLoggingInterceptor is a creator factory for a ResponseLoggingInterceptor
func NewLoggingInterceptor(log *zap.SugaredLogger) fiber.Interceptor {
return &ResponseLoggingInterceptor{
logger: log,
}
}
// ResponseLoggingInterceptor is the structural interceptor used for logging responses
type ResponseLoggingInterceptor struct {
fiber.NoopBeforeDispatchInterceptor
fiber.NoopAfterCompletionInterceptor
logger *zap.SugaredLogger
}
// AfterDispatch logs the success or failure information of a request
func (i *ResponseLoggingInterceptor) AfterDispatch(ctx context.Context, req fiber.Request, queue fiber.ResponseQueue) {
for resp := range queue.Iter() {
if resp.IsSuccess() {
i.logger.Infof("%s: %s", resp.BackendName(), resp.Payload())
} else {
i.logger.Warnf("%s: %s", resp.BackendName(), resp.Payload())
}
}
}