mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 08:16:43 +00:00
- Introduced RouteTag middleware to set route tags for different API endpoints. - Updated logger to include route tags in log output. - Applied RouteTag middleware across various routers including API, dashboard, relay, video, and web routers for consistent logging.
41 lines
820 B
Go
41 lines
820 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const RouteTagKey = "route_tag"
|
|
|
|
func RouteTag(tag string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Set(RouteTagKey, tag)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func SetUpLogger(server *gin.Engine) {
|
|
server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
|
var requestID string
|
|
if param.Keys != nil {
|
|
requestID, _ = param.Keys[common.RequestIdKey].(string)
|
|
}
|
|
tag, _ := param.Keys[RouteTagKey].(string)
|
|
if tag == "" {
|
|
tag = "web"
|
|
}
|
|
return fmt.Sprintf("[GIN] %s | %s | %s | %3d | %13v | %15s | %7s %s\n",
|
|
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
|
tag,
|
|
requestID,
|
|
param.StatusCode,
|
|
param.Latency,
|
|
param.ClientIP,
|
|
param.Method,
|
|
param.Path,
|
|
)
|
|
}))
|
|
}
|