fix: adjust log middleware
This commit is contained in:
@@ -9,7 +9,7 @@ HttpPort = 8000
|
|||||||
ReadTimeout = 60
|
ReadTimeout = 60
|
||||||
WriteTimeout = 60
|
WriteTimeout = 60
|
||||||
; use embed html static file
|
; use embed html static file
|
||||||
; EmbedHtml = disable
|
EmbedHtml = disable
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
Type = mysql
|
Type = mysql
|
||||||
|
|||||||
+57
-53
@@ -2,79 +2,83 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"math"
|
||||||
"message-nest/pkg/logging"
|
"message-nest/pkg/logging"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
//var timeFormat = "02/Jan/2006:15:04:05 -0700"
|
||||||
status200 = 42
|
|
||||||
status404 = 43
|
|
||||||
status500 = 41
|
|
||||||
|
|
||||||
methodGET = 44
|
// LogMiddleware Logger is the logrus logger handler
|
||||||
)
|
func LogMiddleware(notLogged ...string) gin.HandlerFunc {
|
||||||
|
//hostname, err := os.Hostname()
|
||||||
|
//if err != nil {
|
||||||
|
// hostname = "unknown"
|
||||||
|
//}
|
||||||
|
|
||||||
func formatDuration(duration time.Duration) string {
|
var skip map[string]struct{}
|
||||||
seconds := duration.Seconds()
|
|
||||||
switch {
|
if length := len(notLogged); length > 0 {
|
||||||
case seconds >= 1:
|
skip = make(map[string]struct{}, length)
|
||||||
return fmt.Sprintf("%.2fs", seconds)
|
|
||||||
default:
|
for _, p := range notLogged {
|
||||||
return fmt.Sprintf("%.3fms", float64(duration.Milliseconds()))
|
skip[p] = struct{}{}
|
||||||
//default:
|
}
|
||||||
// return fmt.Sprintf("%dns", duration.Nanoseconds())
|
|
||||||
//case duration.Milliseconds() >= 1:
|
|
||||||
// return fmt.Sprintf("%.2fms", float64(duration.Milliseconds()))
|
|
||||||
//default:
|
|
||||||
// return fmt.Sprintf("%dns", duration.Nanoseconds())
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func LogMiddleware() gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
start := time.Now()
|
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
raw := c.Request.URL.RawQuery
|
raw := c.Request.URL.RawQuery
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
// Process request
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// Log only when path is not being skipped
|
stop := time.Since(start)
|
||||||
|
latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000000.0))
|
||||||
// Stop timer
|
|
||||||
end := time.Now()
|
|
||||||
timeSub := end.Sub(start)
|
|
||||||
dr := formatDuration(timeSub)
|
|
||||||
clientIP := c.ClientIP()
|
|
||||||
method := c.Request.Method
|
|
||||||
statusCode := c.Writer.Status()
|
statusCode := c.Writer.Status()
|
||||||
//bodySize := c.Writer.Size()
|
clientIP := c.ClientIP()
|
||||||
|
//clientUserAgent := c.Request.UserAgent()
|
||||||
|
//referer := c.Request.Referer()
|
||||||
|
dataLength := c.Writer.Size()
|
||||||
|
if dataLength < 0 {
|
||||||
|
dataLength = 0
|
||||||
|
}
|
||||||
if raw != "" {
|
if raw != "" {
|
||||||
|
raw, _ := url.QueryUnescape(raw)
|
||||||
path = path + "?" + raw
|
path = path + "?" + raw
|
||||||
}
|
}
|
||||||
|
if _, ok := skip[path]; ok {
|
||||||
var statusColor string
|
return
|
||||||
switch statusCode {
|
|
||||||
case 200:
|
|
||||||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", status200, statusCode)
|
|
||||||
case 500:
|
|
||||||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", status500, statusCode)
|
|
||||||
default:
|
|
||||||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", status404, statusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var methodColor string
|
entry := logging.Logger.WithFields(logrus.Fields{
|
||||||
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodGET, method)
|
//"hostname": hostname,
|
||||||
|
"statusCode": statusCode,
|
||||||
logging.Logger.Infof("[GIN]|%s|%s|%s|%s| %s",
|
"latency": latency, // time to process
|
||||||
//start.Format("2006-01-02 15:04:06"),
|
"clientIP": clientIP,
|
||||||
statusColor,
|
"method": c.Request.Method,
|
||||||
dr,
|
"path": path,
|
||||||
clientIP,
|
//"referer": referer,
|
||||||
methodColor,
|
"dataLength": dataLength,
|
||||||
path,
|
//"userAgent": clientUserAgent,
|
||||||
)
|
})
|
||||||
|
|
||||||
|
if len(c.Errors) > 0 {
|
||||||
|
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
|
||||||
|
} else {
|
||||||
|
msg := fmt.Sprintf("[Gin] %s [%s] %s %d %d (%dms)", clientIP, c.Request.Method, path, statusCode, dataLength, latency)
|
||||||
|
if statusCode >= http.StatusInternalServerError {
|
||||||
|
entry.Error(msg)
|
||||||
|
} else if statusCode >= http.StatusBadRequest {
|
||||||
|
entry.Warn(msg)
|
||||||
|
} else {
|
||||||
|
entry.Info(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (cs *CronService) UpdateLogsCronRun(cron string) {
|
|||||||
Job: ClearLogs,
|
Job: ClearLogs,
|
||||||
})
|
})
|
||||||
logging.Logger.Error(fmt.Sprintf("更新日志的cron成功,%s", cron))
|
logging.Logger.Error(fmt.Sprintf("更新日志的cron成功,%s", cron))
|
||||||
logging.Logger.Info(fmt.Sprintf("所有的定时任务: %s", TaskList))
|
logging.Logger.Error(fmt.Sprintf("所有的定时任务: %s", TaskList))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func AddTask(task ScheduledTask) cron.EntryID {
|
|||||||
logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err))
|
logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err))
|
||||||
} else {
|
} else {
|
||||||
TaskList[taskId] = &task
|
TaskList[taskId] = &task
|
||||||
logging.Logger.Error(fmt.Sprintf("添加日志清除任务成功,entryID: %d, cron: %s", taskId, task.Schedule))
|
logging.Logger.Error(fmt.Sprintf("添加定时任务成功,entryID: %d, cron: %s", taskId, task.Schedule))
|
||||||
}
|
}
|
||||||
return taskId
|
return taskId
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 KiB |
Reference in New Issue
Block a user