[#77] Added requests logging

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-09 12:49:08 +03:00 committed by Stanislav Bogatyrev
parent 9241156af4
commit 2b8aa4914e

17
app.go
View file

@ -197,11 +197,11 @@ func (a *app) Serve(ctx context.Context) {
// Configure router.
r := router.New()
r.RedirectTrailingSlash = true
r.POST("/upload/{cid}", uploader.Upload)
r.POST("/upload/{cid}", a.logger(uploader.Upload))
a.log.Info("added path /upload/{cid}")
r.GET("/get/{cid}/{oid}", downloader.DownloadByAddress)
r.GET("/get/{cid}/{oid}", a.logger(downloader.DownloadByAddress))
a.log.Info("added path /get/{cid}/{oid}")
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", downloader.DownloadByAttribute)
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(downloader.DownloadByAttribute))
a.log.Info("added path /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
// enable metrics
if a.cfg.GetBool(cmdMetrics) {
@ -229,3 +229,14 @@ func (a *app) Serve(ctx context.Context) {
a.log.Fatal("could not start server", zap.Error(err))
}
}
func (a *app) logger(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
a.log.Info("request", zap.String("remote", ctx.RemoteAddr().String()),
zap.ByteString("method", ctx.Method()),
zap.ByteString("path", ctx.Path()),
zap.ByteString("query", ctx.QueryArgs().QueryString()),
zap.Uint64("id", ctx.ID()))
h(ctx)
})
}