feat: 添加日志文件输出功能和心跳故障排查工具

- 新增日志文件输出功能,支持配置日志文件路径和级别
- 添加心跳故障排查脚本 check-heartbeat.sh
- 支持通过环境变量 LOG_FILE 设置日志文件路径
- 日志自动创建目录,支持相对路径和绝对路径
- 优化日志初始化逻辑,支持直接写入文件
- 改进配置加载,支持日志配置项
- 完善文档,添加故障排查章节和日志功能说明
- 更新版本号至 v1.1.0
This commit is contained in:
2025-12-07 16:37:03 +08:00
parent 74c1db2f14
commit d8ea772c24
5 changed files with 745 additions and 13 deletions

View File

@@ -21,6 +21,11 @@ type Config struct {
Interval int `yaml:"interval"` // 心跳间隔(秒)
} `yaml:"heartbeat"`
Log struct {
File string `yaml:"file"` // 日志文件路径(空则输出到标准错误)
Level string `yaml:"level"` // 日志级别debug, info, warn, error默认: info
} `yaml:"log"`
Debug bool `yaml:"debug"`
// 节点信息(通过心跳获取并持久化)
@@ -42,12 +47,13 @@ func Load() (*Config, error) {
cfg.Heartbeat.Interval = 60
cfg.Debug = false
// 从环境变量读取后端URL
backendURL := os.Getenv("BACKEND_URL")
if backendURL == "" {
backendURL = "http://localhost:8080"
// 默认日志配置
logFile := os.Getenv("LOG_FILE")
if logFile == "" {
logFile = "node.log"
}
cfg.Backend.URL = backendURL
cfg.Log.File = logFile
cfg.Log.Level = "info"
// 尝试从配置文件读取
configPath := os.Getenv("CONFIG_PATH")
@@ -66,6 +72,24 @@ func Load() (*Config, error) {
}
}
// 如果配置文件中没有设置日志文件,使用环境变量或默认值
if cfg.Log.File == "" {
logFile := os.Getenv("LOG_FILE")
if logFile == "" {
logFile = "node.log"
}
cfg.Log.File = logFile
}
// 如果配置文件中没有设置日志级别,使用默认值
if cfg.Log.Level == "" {
if cfg.Debug {
cfg.Log.Level = "debug"
} else {
cfg.Log.Level = "info"
}
}
return cfg, nil
}
@@ -102,4 +126,3 @@ func GetConfigPath() string {
}
return configPath
}