From 8f67f778a3d1861ed8e4829f460af22d93b248bd Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Tue, 14 Apr 2026 09:41:02 +0800 Subject: [PATCH] feat(config): add log level configuration and update logging initialization, close #202 --- cmd/run.go | 25 +++++++++++++++++-------- config.example.toml | 6 +++++- config/flags.go | 2 ++ config/log.go | 5 +++++ config/viper.go | 10 ++++++---- 5 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 config/log.go diff --git a/cmd/run.go b/cmd/run.go index 3030411..b1390a2 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -2,9 +2,9 @@ package cmd import ( "context" - "fmt" "os" "path/filepath" + "strings" "time" "slices" @@ -27,14 +27,27 @@ import ( func Run(cmd *cobra.Command, _ []string) { ctx, cancel := context.WithCancel(cmd.Context()) logger := log.NewWithOptions(os.Stdout, log.Options{ - Level: log.DebugLevel, + Level: log.InfoLevel, ReportTimestamp: true, TimeFormat: time.TimeOnly, ReportCaller: true, }) + log.SetDefault(logger) ctx = log.WithContext(ctx, logger) - exitChan, err := initAll(ctx, cmd) + configFile := config.GetConfigFile(cmd) + if err := config.Init(ctx, configFile); err != nil { + logger.Fatal("Init failed", "error", err) + } + + level, err := log.ParseLevel(strings.TrimSpace(config.C().Log.Level)) + if err != nil { + logger.Warn("Invalid log level, fallback to debug", "level", config.C().Log.Level, "error", err) + level = log.DebugLevel + } + logger.SetLevel(level) + + exitChan, err := initAll(ctx) if err != nil { logger.Fatal("Init failed", "error", err) } @@ -51,11 +64,7 @@ func Run(cmd *cobra.Command, _ []string) { cleanCache() } -func initAll(ctx context.Context, cmd *cobra.Command) (<-chan struct{}, error) { - configFile := config.GetConfigFile(cmd) - if err := config.Init(ctx, configFile); err != nil { - return nil, fmt.Errorf("failed to load config: %w", err) - } +func initAll(ctx context.Context) (<-chan struct{}, error) { cache.Init() logger := log.FromContext(ctx) i18n.Init(config.C().Lang) diff --git a/config.example.toml b/config.example.toml index 9abc9c3..11dd746 100644 --- a/config.example.toml +++ b/config.example.toml @@ -5,6 +5,10 @@ retry = 3 # 下载失败重试次数 threads = 4 # 单个任务下载使用的最大线程数 stream = false # 使用流式传输模式, 建议仅在硬盘空间十分有限时使用. +[log] +# 日志级别, 可选: debug, info, warn, error, fatal +level = "debug" + [telegram] # Bot Token # 更换 Bot Token 后请删除会话数据库文件 (默认路径为 data/session.db ) @@ -73,4 +77,4 @@ blacklist = true [[users]] id = 123456 storages = ["本机1"] -blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储 \ No newline at end of file +blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储 diff --git a/config/flags.go b/config/flags.go index 3320649..4c54d91 100644 --- a/config/flags.go +++ b/config/flags.go @@ -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")) diff --git a/config/log.go b/config/log.go new file mode 100644 index 0000000..554a5b4 --- /dev/null +++ b/config/log.go @@ -0,0 +1,5 @@ +package config + +type logConfig struct { + Level string `toml:"level" mapstructure:"level" json:"level"` +} diff --git a/config/viper.go b/config/viper.go index db962c2..43c7ffc 100644 --- a/config/viper.go +++ b/config/viper.go @@ -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,