intial commit
This commit is contained in:
43
middlewares/loggerMiddleware.go
Normal file
43
middlewares/loggerMiddleware.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"nearle/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// ZapLogger is a Fiber middleware that logs HTTP requests using Zap.
|
||||
func ZapLogger() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
start := time.Now()
|
||||
|
||||
// Handle the request
|
||||
err := c.Next()
|
||||
|
||||
latency := time.Since(start).String()
|
||||
status := c.Response().StatusCode()
|
||||
method := c.Method()
|
||||
path := c.Path()
|
||||
ip := c.IP()
|
||||
|
||||
fields := []interface{}{
|
||||
"status", status,
|
||||
"method", method,
|
||||
"path", path,
|
||||
"ip", ip,
|
||||
"latency", latency,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fields = append(fields, "error", err.Error())
|
||||
utils.Logger.Errorw("API Request Failed", fields...)
|
||||
return err
|
||||
}
|
||||
|
||||
utils.Logger.Infow("API Request Successful", fields...)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user