(log *zap.Logger, prefix string, prod bool, adminPass string)
| 9 | ) |
| 10 | |
| 11 | func getAdminLoggerMiddleware(log *zap.Logger, prefix string, prod bool, adminPass string) gin.HandlerFunc { |
| 12 | return func(c *gin.Context) { |
| 13 | if len(adminPass) != 0 && c.Request.Header.Get("X-API-KEY") != adminPass { |
| 14 | c.Status(200) |
| 15 | c.Abort() |
| 16 | return |
| 17 | } |
| 18 | |
| 19 | cid := util.NewUuid() |
| 20 | c.Set(util.STRING_CORRELATION_ID, cid) |
| 21 | logWithCid := log.With(zap.String(util.STRING_CORRELATION_ID, cid)) |
| 22 | util.SetLogToCtx(c, logWithCid) |
| 23 | |
| 24 | start := time.Now() |
| 25 | c.Next() |
| 26 | latency := time.Since(start).Milliseconds() |
| 27 | if !prod { |
| 28 | logWithCid.Sugar().Infof("%s | %d | %s | %s | %dms", prefix, c.Writer.Status(), c.Request.Method, c.FullPath(), latency) |
| 29 | } |
| 30 | |
| 31 | if prod { |
| 32 | logWithCid.Info("request to admin management api", |
| 33 | zap.Int("code", c.Writer.Status()), |
| 34 | zap.String("method", c.Request.Method), |
| 35 | zap.String("path", c.FullPath()), |
| 36 | zap.Int64("lantecyInMs", latency), |
| 37 | ) |
| 38 | } |
| 39 | } |
| 40 | } |
no test coverage detected