增加设置界面、debug日志等级、版本号展示、构建脚本

- 新增设置弹窗: trace开关/正文上限、日志等级(debug/info/warn/error)、并发数、重试次数
- 配置运行时生效,持久化到 config.json
- 日志系统支持 debug 级别过滤
- 工具栏展示版本号(v0.0.2),来源 wails.json + constants.go
- 抢购并发数和重试次数改为读取配置,不再写死常量
- 新增 build.sh 构建脚本,Windows exe 文件名带版本号
- config.json 加入 .gitignore
This commit is contained in:
yml2213
2026-06-24 20:18:33 +08:00
parent 154e55d484
commit 8790c4ca33
10 changed files with 365 additions and 21 deletions
+56 -2
View File
@@ -14,6 +14,29 @@ import (
"douyu_go/internal/constants"
)
// minLevel 日志最低等级,低于此等级的日志不输出。0=info, 1=warn, 2=error。
var minLevel atomic.Int32
// SetMinLevel 设置日志最低显示等级。
func SetMinLevel(level string) {
minLevel.Store(int32(levelPriority(level)))
}
func levelPriority(level string) int {
switch strings.ToLower(level) {
case "debug":
return -1
case "info":
return 0
case "warn":
return 1
case "error":
return 2
default:
return 0
}
}
// AsyncLogWriter 异步日志写入器,队列满时丢弃日志以保护主链路。
type AsyncLogWriter struct {
dst io.Writer
@@ -82,6 +105,10 @@ func InitLogging(store *MemoryLogStore, logDir string) func() {
if logDir == "" {
logDir = constants.DefaultLogDir
}
// 解析为绝对路径,避免打包后工作目录不同导致日志写到意外位置
logDir = resolveAbsolutePath(logDir)
if err := os.MkdirAll(logDir, 0755); err != nil {
async := NewAsyncLogWriter(io.MultiWriter(os.Stdout, store), 2048)
log.SetOutput(async)
@@ -108,7 +135,7 @@ func InitLogging(store *MemoryLogStore, logDir string) func() {
}
}
// LogEvent 输出带分类和级别的结构化文本日志。
// LogEvent 输出带分类和级别的结构化文本日志。低于当前最低等级的日志会被丢弃。
func LogEvent(category, level, format string, args ...any) {
if category == "" {
category = "系统"
@@ -116,6 +143,9 @@ func LogEvent(category, level, format string, args ...any) {
if level == "" {
level = "info"
}
if levelPriority(level) < int(minLevel.Load()) {
return
}
log.Printf("[%s][%s] %s", category, level, fmt.Sprintf(format, args...))
}
@@ -150,17 +180,41 @@ func cutBracket(s string) (string, string, bool) {
// IsLogLevel 判断是否为有效日志级别。
func IsLogLevel(level string) bool {
switch strings.ToLower(level) {
case "success", "error", "warn", "info":
case "success", "error", "warn", "info", "debug":
return true
default:
return false
}
}
// LogDebug 输出 debug 级别日志,仅在日志等级设为 debug 时可见。
func LogDebug(category, format string, args ...any) {
LogEvent(category, "debug", format, args...)
}
// resolveAbsolutePath 将相对路径解析为绝对路径。
// 如果路径已经是绝对路径则直接返回;否则基于可执行文件所在目录解析。
func resolveAbsolutePath(path string) string {
if filepath.IsAbs(path) {
return path
}
// 先尝试基于当前工作目录解析
if abs, err := filepath.Abs(path); err == nil {
return abs
}
// 兜底:基于可执行文件所在目录
if exe, err := os.Executable(); err == nil {
return filepath.Join(filepath.Dir(exe), path)
}
return path
}
// DetectLogLevel 根据日志文本粗略分类,方便前端染色。
func DetectLogLevel(line string) string {
lower := strings.ToLower(line)
switch {
case strings.Contains(lower, "[debug]"):
return "debug"
case strings.Contains(lower, "[success]"):
return "success"
case strings.Contains(lower, "[error]"):