6066f43a7d
- Implement monitoring service with heartbeat, lease tasks, resume tasks, and task result handling. - Create monitoring time utilities for business date calculations. - Add unit tests for date window resolution and business day handling. - Define database schema for monitoring-related tables including quotas, daily reports, and task management. - Establish migration scripts for creating and dropping monitoring tables.
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
)
|
|
|
|
func Logger(logger *zap.Logger) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
c.Next()
|
|
|
|
fields := []zap.Field{
|
|
zap.String("method", c.Request.Method),
|
|
zap.String("path", c.Request.URL.Path),
|
|
zap.Int("status", c.Writer.Status()),
|
|
zap.Duration("latency", time.Since(start)),
|
|
zap.String("request_id", RequestIDFromGin(c)),
|
|
zap.String("client_ip", c.ClientIP()),
|
|
}
|
|
|
|
if appErr, ok := response.AppErrorFromGin(c); ok {
|
|
fields = append(fields,
|
|
zap.Int("app_code", appErr.Code),
|
|
zap.String("app_message", appErr.Message),
|
|
)
|
|
if appErr.Detail != "" {
|
|
fields = append(fields, zap.String("app_detail", appErr.Detail))
|
|
}
|
|
if appErr.Cause != nil {
|
|
fields = append(fields, zap.Error(appErr.Cause))
|
|
}
|
|
} else if len(c.Errors) > 0 {
|
|
fields = append(fields, zap.String("error", c.Errors.String()))
|
|
}
|
|
|
|
switch status := c.Writer.Status(); {
|
|
case status >= 500:
|
|
logger.Error("request", fields...)
|
|
case status >= 400:
|
|
logger.Warn("request", fields...)
|
|
default:
|
|
logger.Info("request", fields...)
|
|
}
|
|
}
|
|
}
|