Fix: gin log error levle

This commit is contained in:
zijiren233 2024-05-23 19:03:18 +08:00
parent 39f097d151
commit 636b32d7f8
1 changed files with 13 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package middlewares
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
@ -42,7 +43,18 @@ func NewLog(l *logrus.Logger) gin.HandlerFunc {
param.Path = path
c.MustGet("log").(*logrus.Entry).Info(formatter(param))
logColor(c.MustGet("log").(*logrus.Entry), param)
}
}
func logColor(logger *logrus.Entry, p gin.LogFormatterParams) {
str := formatter(p)
code := p.StatusCode
switch {
case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
logger.Error(str)
default:
logger.Info(str)
}
}