feat: 添加配置加载和日志配置支持
- 新增 LogConfig 结构体,支持 9 个日志配置字段(level, encoding, output_stdout, file_path, max_size, max_backups, max_age, compress, gorm_level) - Config 结构体新增 Log 字段,支持 YAML 解析 - output_stdout 使用 *bool 指针类型,nil 默认为 true - 更新 config.example.yaml 添加完整 log 配置段 - 新增 TDD 测试:日志配置解析、向后兼容、字段完整性 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
51
internal/config/config.go
Normal file
51
internal/config/config.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// LogConfig holds logging configuration values.
|
||||
type LogConfig struct {
|
||||
Level string `yaml:"level"` // debug, info, warn, error (default: info)
|
||||
Encoding string `yaml:"encoding"` // json, console (default: json)
|
||||
OutputStdout *bool `yaml:"output_stdout"` // 输出到终端 (default: true)
|
||||
FilePath string `yaml:"file_path"` // log file path (for rotation)
|
||||
MaxSize int `yaml:"max_size"` // MB per file (default: 100)
|
||||
MaxBackups int `yaml:"max_backups"` // retained files (default: 5)
|
||||
MaxAge int `yaml:"max_age"` // days to retain (default: 30)
|
||||
Compress bool `yaml:"compress"` // gzip old files (default: true)
|
||||
GormLevel string `yaml:"gorm_level"` // GORM SQL log level (default: warn)
|
||||
}
|
||||
|
||||
// Config holds all application configuration values.
|
||||
type Config struct {
|
||||
ServerPort string `yaml:"server_port"`
|
||||
SlurmAPIURL string `yaml:"slurm_api_url"`
|
||||
SlurmUserName string `yaml:"slurm_user_name"`
|
||||
SlurmJWTKeyPath string `yaml:"slurm_jwt_key_path"`
|
||||
MySQLDSN string `yaml:"mysql_dsn"`
|
||||
Log LogConfig `yaml:"log"`
|
||||
}
|
||||
|
||||
// Load reads a YAML configuration file and returns a parsed Config.
|
||||
// If path is empty, it defaults to "./config.yaml".
|
||||
func Load(path string) (*Config, error) {
|
||||
if path == "" {
|
||||
path = "./config.yaml"
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read config file %s: %w", path, err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("parse config file %s: %w", path, err)
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user