优化日志系统

This commit is contained in:
yml2213
2026-06-19 12:10:55 +08:00
parent 1805edc63a
commit 59b9dd12ef
11 changed files with 232 additions and 66 deletions
+11 -10
View File
@@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
@@ -14,11 +13,11 @@ func (s *appState) runNow(accountProducts map[string]string) {
s.clearResults()
attempts := s.buildAttempts(accountProducts)
if len(attempts) == 0 {
log.Printf("没有可执行的抢购任务")
logEvent("抢购", "warn", "没有可执行的抢购任务")
return
}
log.Printf("开始抢购: %d 个请求", len(attempts))
logEvent("抢购", "info", "开始抢购: %d 个请求", len(attempts))
limit := make(chan struct{}, defaultConcurrency)
var wg sync.WaitGroup
for _, attempt := range attempts {
@@ -32,15 +31,15 @@ func (s *appState) runNow(accountProducts map[string]string) {
key := fmt.Sprintf("%s_第%d包", result.Account, result.Attempt)
s.setResult(key, result.Message)
if result.OK {
log.Printf("成功: %s %s 第%d包 %s", result.Account, result.Product, result.Attempt, result.Message)
logEvent("抢购", "success", "成功: %s %s 第%d包 %s", result.Account, result.Product, result.Attempt, result.Message)
go s.saveSuccessImage(attempt.Account, attempt.Product, result.RoleName)
} else {
log.Printf("失败: %s %s 第%d包 %s", result.Account, result.Product, result.Attempt, result.Message)
logEvent("抢购", "error", "失败: %s %s 第%d包 %s", result.Account, result.Product, result.Attempt, result.Message)
}
}(attempt)
}
wg.Wait()
log.Printf("抢购任务结束")
logEvent("抢购", "info", "抢购任务结束")
}
// buildAttempts 根据前端传入的账号到商品映射,生成具体兑换尝试。
@@ -81,6 +80,7 @@ func (s *appState) runAttempt(attempt ExchangeAttempt) ExchangeResult {
Attempt: attempt.Index,
}
logEvent("抢购", "info", "执行兑换链路: 账号=%s 商品=%s 第%d包", attempt.Account.Name, attempt.Product.CommodityName, attempt.Index)
limitCSRF, err := s.client.GenerateCSRF(attempt.Account.Cookie)
if err != nil {
result.Message = "失败|生成CSRF失败: " + err.Error()
@@ -96,6 +96,7 @@ func (s *appState) runAttempt(attempt ExchangeAttempt) ExchangeResult {
result.Message = "失败|当前商品限制兑换"
return result
}
logEvent("抢购", "info", "批量限制通过: 账号=%s 商品=%s limit=%d", attempt.Account.Name, attempt.Product.CommodityName, limit)
preCSRF, err := s.client.GenerateCSRF(attempt.Account.Cookie)
if err != nil {
@@ -120,7 +121,7 @@ func (s *appState) runAttempt(attempt ExchangeAttempt) ExchangeResult {
roleName, err := s.client.RoleName(attempt.Account.Cookie)
if err != nil {
log.Printf("%s 角色名查询失败: %v", attempt.Account.Name, err)
logEvent("抢购", "warn", "%s 角色名查询失败: %v", attempt.Account.Name, err)
}
result.OK = true
@@ -150,7 +151,7 @@ func (s *appState) scheduleRun(req SeckillRequest) (map[string]any, error) {
serverTime, err := s.client.ServerTime()
if err != nil {
log.Printf("定时使用降级时间: %v", err)
logEvent("定时", "warn", "定时使用降级时间: %v", err)
}
advance := time.Duration(req.Advance) * time.Millisecond
@@ -166,12 +167,12 @@ func (s *appState) scheduleRun(req SeckillRequest) (map[string]any, error) {
s.schedule.Stop()
}
s.schedule = time.AfterFunc(remain, func() {
log.Printf("定时触发: 本地=%s", time.Now().In(cst).Format("15:04:05.000"))
logEvent("定时", "success", "定时触发: 本地=%s", time.Now().In(cst).Format("15:04:05.000"))
s.runNow(ap)
})
s.scheduleMu.Unlock()
log.Printf("定时已设置: 目标=%s 触发=%s 提前=%dms",
logEvent("定时", "success", "定时已设置: 目标=%s 触发=%s 提前=%dms",
targetTime.In(cst).Format("2006-01-02 15:04:05.000"),
triggerTime.In(cst).Format("2006-01-02 15:04:05.000"),
req.Advance,