Revert "feat(log): add error logging middleware for improved error handling (#182)"

This reverts commit 5e8d8d070a.
This commit is contained in:
j2rong4cn
2025-06-19 09:56:45 +08:00
parent 79521db8e0
commit 70b937e031
2 changed files with 0 additions and 58 deletions

View File

@ -19,7 +19,6 @@ import (
"github.com/OpenListTeam/OpenList/internal/fs"
"github.com/OpenListTeam/OpenList/pkg/utils"
"github.com/OpenListTeam/OpenList/server"
"github.com/OpenListTeam/OpenList/server/middlewares"
"github.com/OpenListTeam/sftpd-openlist"
ftpserver "github.com/fclairamb/ftpserverlib"
"github.com/gin-gonic/gin"
@ -48,7 +47,6 @@ the address is defined in config file`,
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(middlewares.ErrorLogging())
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
server.Init(r)
var httpHandler http.Handler = r

View File

@ -1,56 +0,0 @@
package middlewares
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func ErrorLogging() gin.HandlerFunc {
return func(c *gin.Context) {
w := &responseBodyWriter{body: &bytes.Buffer{}, ResponseWriter: c.Writer}
c.Writer = w
c.Next()
var errorMsg string
if w.body.Len() > 0 {
var jsonBody struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal(w.body.Bytes(), &jsonBody); err == nil {
if jsonBody.Code != 200 {
errorMsg = fmt.Sprintf(" error: code=%d, message=%s", jsonBody.Code, jsonBody.Message)
}
}
}
if c.Writer.Status() >= 400 {
if len(c.Errors) > 0 {
errorMsg = c.Errors.String()
} else if errorMsg == "" && w.body.Len() > 0 {
body := w.body.String()
if len(body) > 500 {
errorMsg = body[:500] + "..."
} else {
errorMsg = body
}
}
}
if errorMsg != "" {
log.Error(errorMsg)
}
}
}
type responseBodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (r *responseBodyWriter) Write(b []byte) (int, error) {
r.body.Write(b)
return r.ResponseWriter.Write(b)
}