Files
geo/server/internal/shared/middleware/logger.go
T
root 446f865cdf feat: add knowledge management functionality with CRUD operations and database schema
- Implemented KnowledgeHandler for managing knowledge groups and items, including listing, creating, updating, and deleting operations.
- Added database migration scripts to create necessary tables for knowledge management, including knowledge_groups, knowledge_items, knowledge_parse_tasks, and knowledge_chunks_meta.
- Introduced prompt_rule_knowledge_groups table to associate prompt rules with knowledge groups.
2026-04-05 17:14:13 +08:00

48 lines
1.1 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))
}
} 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...)
}
}
}