mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-06 22:51:21 +08:00
feat(config): add log level configuration and update logging initialization, close #202
This commit is contained in:
25
cmd/run.go
25
cmd/run.go
@@ -2,9 +2,9 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
@@ -27,14 +27,27 @@ import (
|
|||||||
func Run(cmd *cobra.Command, _ []string) {
|
func Run(cmd *cobra.Command, _ []string) {
|
||||||
ctx, cancel := context.WithCancel(cmd.Context())
|
ctx, cancel := context.WithCancel(cmd.Context())
|
||||||
logger := log.NewWithOptions(os.Stdout, log.Options{
|
logger := log.NewWithOptions(os.Stdout, log.Options{
|
||||||
Level: log.DebugLevel,
|
Level: log.InfoLevel,
|
||||||
ReportTimestamp: true,
|
ReportTimestamp: true,
|
||||||
TimeFormat: time.TimeOnly,
|
TimeFormat: time.TimeOnly,
|
||||||
ReportCaller: true,
|
ReportCaller: true,
|
||||||
})
|
})
|
||||||
|
log.SetDefault(logger)
|
||||||
ctx = log.WithContext(ctx, 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 {
|
if err != nil {
|
||||||
logger.Fatal("Init failed", "error", err)
|
logger.Fatal("Init failed", "error", err)
|
||||||
}
|
}
|
||||||
@@ -51,11 +64,7 @@ func Run(cmd *cobra.Command, _ []string) {
|
|||||||
cleanCache()
|
cleanCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
func initAll(ctx context.Context, cmd *cobra.Command) (<-chan struct{}, error) {
|
func initAll(ctx context.Context) (<-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)
|
|
||||||
}
|
|
||||||
cache.Init()
|
cache.Init()
|
||||||
logger := log.FromContext(ctx)
|
logger := log.FromContext(ctx)
|
||||||
i18n.Init(config.C().Lang)
|
i18n.Init(config.C().Lang)
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ retry = 3 # 下载失败重试次数
|
|||||||
threads = 4 # 单个任务下载使用的最大线程数
|
threads = 4 # 单个任务下载使用的最大线程数
|
||||||
stream = false # 使用流式传输模式, 建议仅在硬盘空间十分有限时使用.
|
stream = false # 使用流式传输模式, 建议仅在硬盘空间十分有限时使用.
|
||||||
|
|
||||||
|
[log]
|
||||||
|
# 日志级别, 可选: debug, info, warn, error, fatal
|
||||||
|
level = "debug"
|
||||||
|
|
||||||
[telegram]
|
[telegram]
|
||||||
# Bot Token
|
# Bot Token
|
||||||
# 更换 Bot Token 后请删除会话数据库文件 (默认路径为 data/session.db )
|
# 更换 Bot Token 后请删除会话数据库文件 (默认路径为 data/session.db )
|
||||||
@@ -73,4 +77,4 @@ blacklist = true
|
|||||||
[[users]]
|
[[users]]
|
||||||
id = 123456
|
id = 123456
|
||||||
storages = ["本机1"]
|
storages = ["本机1"]
|
||||||
blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储
|
blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ func RegisterFlags(cmd *cobra.Command) {
|
|||||||
flags.Bool("stream", false, "enable stream mode")
|
flags.Bool("stream", false, "enable stream mode")
|
||||||
flags.Bool("no-clean-cache", false, "do not clean cache on exit")
|
flags.Bool("no-clean-cache", false, "do not clean cache on exit")
|
||||||
flags.String("proxy", "", "proxy URL (http, https, socks5, socks5h)")
|
flags.String("proxy", "", "proxy URL (http, https, socks5, socks5h)")
|
||||||
|
flags.String("log-level", "", "log level (trace/debug, info, warn, error, fatal)")
|
||||||
|
|
||||||
// Telegram 配置
|
// Telegram 配置
|
||||||
flags.String("telegram-token", "", "telegram bot token")
|
flags.String("telegram-token", "", "telegram bot token")
|
||||||
@@ -54,6 +55,7 @@ func bindFlags(cmd *cobra.Command) {
|
|||||||
viper.BindPFlag("stream", flags.Lookup("stream"))
|
viper.BindPFlag("stream", flags.Lookup("stream"))
|
||||||
viper.BindPFlag("no_clean_cache", flags.Lookup("no-clean-cache"))
|
viper.BindPFlag("no_clean_cache", flags.Lookup("no-clean-cache"))
|
||||||
viper.BindPFlag("proxy", flags.Lookup("proxy"))
|
viper.BindPFlag("proxy", flags.Lookup("proxy"))
|
||||||
|
viper.BindPFlag("log.level", flags.Lookup("log-level"))
|
||||||
|
|
||||||
// Telegram
|
// Telegram
|
||||||
viper.BindPFlag("telegram.token", flags.Lookup("telegram-token"))
|
viper.BindPFlag("telegram.token", flags.Lookup("telegram-token"))
|
||||||
|
|||||||
5
config/log.go
Normal file
5
config/log.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type logConfig struct {
|
||||||
|
Level string `toml:"level" mapstructure:"level" json:"level"`
|
||||||
|
}
|
||||||
@@ -23,6 +23,7 @@ type Config struct {
|
|||||||
Threads int `toml:"threads" mapstructure:"threads" json:"threads"`
|
Threads int `toml:"threads" mapstructure:"threads" json:"threads"`
|
||||||
Stream bool `toml:"stream" mapstructure:"stream" json:"stream"`
|
Stream bool `toml:"stream" mapstructure:"stream" json:"stream"`
|
||||||
Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
|
Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
|
||||||
|
Log logConfig `toml:"log" mapstructure:"log" json:"log"`
|
||||||
Aria2 aria2Config `toml:"aria2" mapstructure:"aria2" json:"aria2"`
|
Aria2 aria2Config `toml:"aria2" mapstructure:"aria2" json:"aria2"`
|
||||||
API apiConfig `toml:"api" mapstructure:"api" json:"api"`
|
API apiConfig `toml:"api" mapstructure:"api" json:"api"`
|
||||||
|
|
||||||
@@ -100,10 +101,11 @@ func Init(ctx context.Context, configFile ...string) error {
|
|||||||
|
|
||||||
defaultConfigs := map[string]any{
|
defaultConfigs := map[string]any{
|
||||||
// 基础配置
|
// 基础配置
|
||||||
"lang": "zh-Hans",
|
"lang": "zh-Hans",
|
||||||
"workers": 3,
|
"workers": 3,
|
||||||
"retry": 3,
|
"retry": 3,
|
||||||
"threads": 4,
|
"threads": 4,
|
||||||
|
"log.level": "debug",
|
||||||
|
|
||||||
// 缓存配置
|
// 缓存配置
|
||||||
"cache.ttl": 86400,
|
"cache.ttl": 86400,
|
||||||
|
|||||||
Reference in New Issue
Block a user