From 2b8aa4914e655eb717086b9ed99116aa967941b9 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 9 Jul 2021 12:49:08 +0300 Subject: [PATCH] [#77] Added requests logging Signed-off-by: Denis Kirillov --- app.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index 0fbc312..2e53a13 100644 --- a/app.go +++ b/app.go @@ -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) + }) +}