feat(config): add log level configuration and update logging initialization, close #202

This commit is contained in:
krau
2026-04-14 09:41:02 +08:00
parent 159dba6224
commit 8f67f778a3
5 changed files with 35 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ func RegisterFlags(cmd *cobra.Command) {
flags.Bool("stream", false, "enable stream mode")
flags.Bool("no-clean-cache", false, "do not clean cache on exit")
flags.String("proxy", "", "proxy URL (http, https, socks5, socks5h)")
flags.String("log-level", "", "log level (trace/debug, info, warn, error, fatal)")
// Telegram 配置
flags.String("telegram-token", "", "telegram bot token")
@@ -54,6 +55,7 @@ func bindFlags(cmd *cobra.Command) {
viper.BindPFlag("stream", flags.Lookup("stream"))
viper.BindPFlag("no_clean_cache", flags.Lookup("no-clean-cache"))
viper.BindPFlag("proxy", flags.Lookup("proxy"))
viper.BindPFlag("log.level", flags.Lookup("log-level"))
// Telegram
viper.BindPFlag("telegram.token", flags.Lookup("telegram-token"))

5
config/log.go Normal file
View File

@@ -0,0 +1,5 @@
package config
type logConfig struct {
Level string `toml:"level" mapstructure:"level" json:"level"`
}

View File

@@ -23,6 +23,7 @@ type Config struct {
Threads int `toml:"threads" mapstructure:"threads" json:"threads"`
Stream bool `toml:"stream" mapstructure:"stream" json:"stream"`
Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
Log logConfig `toml:"log" mapstructure:"log" json:"log"`
Aria2 aria2Config `toml:"aria2" mapstructure:"aria2" json:"aria2"`
API apiConfig `toml:"api" mapstructure:"api" json:"api"`
@@ -100,10 +101,11 @@ func Init(ctx context.Context, configFile ...string) error {
defaultConfigs := map[string]any{
// 基础配置
"lang": "zh-Hans",
"workers": 3,
"retry": 3,
"threads": 4,
"lang": "zh-Hans",
"workers": 3,
"retry": 3,
"threads": 4,
"log.level": "debug",
// 缓存配置
"cache.ttl": 86400,