优化日志输出信息,调整 Docker Compose 配置,新增 Vector 日志收集配置

This commit is contained in:
2025-06-23 16:28:02 +08:00
parent fda1a2de0e
commit 20aa3d929c
9 changed files with 139 additions and 58 deletions

View File

@@ -10,14 +10,10 @@ import (
"log/slog"
"net/http"
_ "net/http/pprof"
"platform/pkg/u"
"platform/web/auth"
g "platform/web/globals"
"platform/web/globals/orm"
m "platform/web/models"
q "platform/web/queries"
"runtime"
"strconv"
"strings"
"time"
)
@@ -74,13 +70,13 @@ func (s *Server) Run() error {
}()
// listen
slog.Info("Server started on :8080")
slog.Info("服务开始监听 :8080")
err := s.fiber.Listen("0.0.0.0:8080")
if err != nil {
slog.Error("Failed to start server", slog.Any("err", err))
}
slog.Info("Server stopped")
slog.Info("服务已停止")
return nil
}
@@ -121,55 +117,42 @@ func newLogger() fiber.Handler {
TimeFormat: "2006-01-02 15:04:05",
TimeZone: "Asia/Shanghai",
Next: func(c *fiber.Ctx) bool {
c.Locals("authtype", auth.PayloadNone.ToStr())
c.Locals("authid", 0)
authCtx, ok := c.Locals("auth").(*auth.Context)
if ok {
c.Locals("authtype", authCtx.Payload.Type.ToStr())
c.Locals("authid", authCtx.Payload.Id)
} else {
c.Locals("authtype", auth.PayloadNone.ToStr())
c.Locals("authid", 0)
}
return false
},
Done: func(c *fiber.Ctx, logBytes []byte) {
go func(ip, ua, method, path string, status int, logBytes []byte) {
var logStr = strings.TrimPrefix(string(logBytes), "🚀")
var logVars = strings.Split(logStr, "|")
var logStr = strings.TrimPrefix(string(logBytes), "🚀")
var logVars = strings.Split(logStr, "|")
var reqTimeStr = strings.TrimSpace(logVars[0])
reqTime, err := time.ParseInLocation("2006-01-02 15:04:05", reqTimeStr, time.Local)
if err != nil {
slog.Error("时间解析错误", slog.Any("err", err))
return
}
var reqTimeStr = strings.TrimSpace(logVars[0])
reqTime, err := time.ParseInLocation("2006-01-02 15:04:05", reqTimeStr, time.Local)
if err != nil {
slog.Error("时间解析错误", slog.Any("err", err))
return
}
var authInfo = strings.Split(strings.TrimSpace(logVars[1]), " ")
var authType = auth.PayloadTypeFromStr(strings.TrimSpace(authInfo[0]))
authID, err := strconv.Atoi(strings.TrimSpace(authInfo[1]))
if err != nil {
slog.Error("负载ID解析错误", slog.Any("err", err))
return
}
var latency = strings.TrimSpace(logVars[4])
var errStr = strings.TrimSpace(logVars[5])
var latency = strings.TrimSpace(logVars[4])
var errStr = strings.TrimSpace(logVars[5])
var item = &m.LogsRequest{
Identity: int32(authType),
IP: ip,
Ua: u.P(ua),
Method: method,
Path: path,
Latency: latency,
Status: int32(status),
Error: &errStr,
Time: orm.LocalDateTime(reqTime),
}
if authID != 0 {
item.Visitor = u.P(int32(authID))
}
err = q.LogsRequest.Create(item)
if err != nil {
slog.Error("日志记录错误", slog.Any("err", err))
return
}
}(c.IP(), c.Get("User-Agent"), c.Method(), c.Path(), c.Response().StatusCode(), logBytes)
slog.Info("接口请求",
slog.String("identity", c.Locals("authtype").(string)),
slog.Int("visitor", c.Locals("authid").(int)),
slog.String("ip", c.IP()),
slog.String("ua", c.Get("User-Agent")),
slog.String("method", c.Method()),
slog.String("path", c.Path()),
slog.Int("status", c.Response().StatusCode()),
slog.String("error", errStr),
slog.String("latency", latency),
slog.Time("time", reqTime),
)
},
})
}