-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (39 loc) · 1.06 KB
/
main.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
package main
import (
"log"
"github.com/fasthttp/router"
"github.com/gowo9/fhlogger/fhzap"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// init zap logger
zapConfig := zap.NewDevelopmentConfig()
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
zapLogger, err := zapConfig.Build()
if err != nil {
log.Fatalf("zap.NewProduction failed, err=%s", err)
}
// init fhzap
fhZap := fhzap.New(zapLogger,
// specify ignore path
fhzap.WithSkipPaths([]string{"/no-log", "/favicon.ico"}),
)
// init router
r := router.New()
r.GET("/foo", func(ctx *fasthttp.RequestCtx) {
_, _ = ctx.WriteString("foo")
})
r.GET("/bar", func(ctx *fasthttp.RequestCtx) {
_, _ = ctx.WriteString("bar")
})
r.GET("/no-log", func(ctx *fasthttp.RequestCtx) {
_, _ = ctx.WriteString("no-log")
})
// combined middleware and listen
finalHandler := fhZap.Combined(r.Handler)
if err = fasthttp.ListenAndServe("127.0.0.1:8080", finalHandler); err != nil {
log.Fatalf("fasthttp.ListenAndServe failed, err=%s", err)
}
}