优化日志输出信息,调整 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

@@ -98,8 +98,6 @@ func Protect(c *fiber.Ctx, types []PayloadType, permissions []string) (*Context,
func Locals(c *fiber.Ctx, auth *Context) {
c.Locals("auth", auth)
c.Locals("authtype", auth.Payload.Type.ToStr())
c.Locals("authid", auth.Payload.Id)
}
func authBearer(ctx context.Context, token string) (*Context, error) {

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"platform/pkg/u"
"platform/web/core"
bill2 "platform/web/domains/bill"
resource2 "platform/web/domains/resource"
trade2 "platform/web/domains/trade"
@@ -124,7 +125,7 @@ func (s *resourceService) PrepareResource(uid int32, now time.Time, ser *Prepare
return err
}
err = g.Redis.Set(context.Background(), result.TradeNo, &PrepareResourceCache{
err = g.Redis.Set(context.Background(), resPrepareKey(result.TradeNo), &PrepareResourceCache{
Uid: uid,
TradeId: result.Trade.ID,
BillId: result.Bill.ID,
@@ -146,9 +147,9 @@ func (s *resourceService) PrepareResource(uid int32, now time.Time, ser *Prepare
func (s *resourceService) CompleteResource(tradeNo string, now time.Time, opResult ...*TradeSuccessResult) error {
// 获取请求缓存
reqStr, err := g.Redis.Get(context.Background(), tradeNo).Result()
reqStr, err := g.Redis.Get(context.Background(), resPrepareKey(tradeNo)).Result()
if err != nil {
return err
return core.NewBizErr("交易不存在或已过期")
}
cache := new(PrepareResourceCache)
if err := json.Unmarshal([]byte(reqStr), cache); err != nil {
@@ -293,6 +294,10 @@ func createResource(q *q.Query, uid int32, now time.Time, data CreateTypeResourc
return &resource, nil
}
func resPrepareKey(tradeNo string) string {
return fmt.Sprintf("resource:prepare:%s", tradeNo)
}
type CreateTypeResourceDataInter interface {
GetName() string
GetPrice() decimal.Decimal

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),
)
},
})
}