调整gin错误日志输出

This commit is contained in:
walker 2024-01-31 16:56:48 +08:00
parent ddc38eb2cb
commit 570448606e

View File

@ -5,11 +5,9 @@ import (
"log/slog" "log/slog"
"net" "net"
"net/http" "net/http"
"net/http/httputil"
"os" "os"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time"
"joylink.club/bj-rtsts-server/db/dbquery" "joylink.club/bj-rtsts-server/db/dbquery"
"joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/dto"
@ -54,6 +52,7 @@ func InitServer() *gin.Engine {
be = sys_error.New("未知错误", fmt.Errorf("%v", e)) be = sys_error.New("未知错误", fmt.Errorf("%v", e))
} }
} }
c.Error(be)
c.JSON(http.StatusInternalServerError, &dto.ErrorDto{ c.JSON(http.StatusInternalServerError, &dto.ErrorDto{
Tip: be.UserMsg, Tip: be.UserMsg,
Message: be.Error(), Message: be.Error(),
@ -79,11 +78,9 @@ func CustomRecoveryWithSlog(logger *slog.Logger, stack bool, recovery gin.Recove
} }
} }
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe { if brokenPipe {
logger.Error(c.Request.URL.Path, logger.Error(c.Request.URL.Path,
"error", err, "error", err,
"request", string(httpRequest),
) )
// If the connection is dead, we can't write a status to it. // If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck c.Error(err.(error)) // nolint: errcheck
@ -93,17 +90,13 @@ func CustomRecoveryWithSlog(logger *slog.Logger, stack bool, recovery gin.Recove
if stack { if stack {
logger.Error("请求处理Panic异常", logger.Error("请求处理Panic异常",
"time", time.Now(),
"error", err, "error", err,
"request", string(httpRequest),
"stack", string(debug.Stack()), "stack", string(debug.Stack()),
) )
debug.PrintStack() debug.PrintStack()
} else { } else {
logger.Error("请求处理Panic异常", logger.Error("请求处理Panic异常",
"time", time.Now(),
"error", err, "error", err,
"request", string(httpRequest),
) )
} }
recovery(c, err) recovery(c, err)