You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
package ginLog
|
|
|
|
import (
|
|
"cynking.com/pkg/log"
|
|
"github.com/gin-gonic/gin"
|
|
"time"
|
|
)
|
|
|
|
func NamedLogger(msg string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Start timer
|
|
start := time.Now()
|
|
path := c.Request.URL.Path
|
|
raw := c.Request.URL.RawQuery
|
|
|
|
// Process request
|
|
c.Next()
|
|
|
|
param := gin.LogFormatterParams{
|
|
Request: c.Request,
|
|
Keys: c.Keys,
|
|
}
|
|
|
|
// Stop timer
|
|
param.TimeStamp = time.Now()
|
|
param.Latency = param.TimeStamp.Sub(start)
|
|
|
|
param.ClientIP = c.ClientIP()
|
|
param.Method = c.Request.Method
|
|
param.StatusCode = c.Writer.Status()
|
|
param.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String()
|
|
param.BodySize = c.Writer.Size()
|
|
|
|
if raw != "" {
|
|
path = path + "?" + raw
|
|
}
|
|
param.Path = path
|
|
// todo:请求处理过长时间提升日志等级 debug(正常),info(慢速),warn(可能浏览器已判定超时)处理
|
|
log.Debug(msg,
|
|
log.Int("Status", param.StatusCode),
|
|
log.Duration("Latency", param.Latency),
|
|
log.String("Ip", param.ClientIP),
|
|
log.String("Method", param.Method),
|
|
log.String("Path", param.Path),
|
|
log.String("ErrorMessage", param.ErrorMessage))
|
|
}
|
|
}
|