feat: add new features such as database watchdog
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func slogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
query := c.Request.URL.RawQuery
|
||||
|
||||
c.Next()
|
||||
|
||||
logger.Info("request",
|
||||
slog.String("method", c.Request.Method),
|
||||
slog.String("path", path),
|
||||
slog.String("query", query),
|
||||
slog.Int("status", c.Writer.Status()),
|
||||
slog.Duration("latency", time.Since(start)),
|
||||
slog.String("client_ip", c.ClientIP()),
|
||||
slog.Int("body_size", c.Writer.Size()),
|
||||
)
|
||||
|
||||
if len(c.Errors) > 0 {
|
||||
for _, err := range c.Errors {
|
||||
logger.Error("request error", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user