绑定日志与命令行关联
This commit is contained in:
@@ -3,9 +3,11 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/user/devpack/internal/logging"
|
||||||
"github.com/user/devpack/pkg/version"
|
"github.com/user/devpack/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +39,11 @@ func Execute() error {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
|
// 在所有子命令执行前初始化日志系统
|
||||||
|
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
||||||
|
return initLogging()
|
||||||
|
}
|
||||||
|
|
||||||
// 全局参数
|
// 全局参数
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "配置文件路径 (默认: ~/.devpack/config.yaml)")
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "配置文件路径 (默认: ~/.devpack/config.yaml)")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "详细输出")
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "详细输出")
|
||||||
@@ -78,3 +85,39 @@ func initConfig() {
|
|||||||
// 读取配置文件(文件不存在时忽略错误)
|
// 读取配置文件(文件不存在时忽略错误)
|
||||||
_ = viper.ReadInConfig()
|
_ = viper.ReadInConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initLogging 根据 CLI 参数和配置文件初始化日志系统
|
||||||
|
func initLogging() error {
|
||||||
|
// CLI flags 优先,未指定时从 viper 配置读取
|
||||||
|
level := logLevel
|
||||||
|
if !rootCmd.PersistentFlags().Changed("log-level") {
|
||||||
|
if v := viper.GetString("log_level"); v != "" {
|
||||||
|
level = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 日志文件路径:默认写入 ~/.devpack/logs/devpack.log
|
||||||
|
logFile := viper.GetString("log_file")
|
||||||
|
if logFile == "" {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err == nil {
|
||||||
|
logFile = filepath.Join(home, ".devpack", "logs", "devpack.log")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := logging.Init(logging.Options{
|
||||||
|
Level: level,
|
||||||
|
LogFile: logFile,
|
||||||
|
Verbose: verbose,
|
||||||
|
Quiet: quiet,
|
||||||
|
NoColor: noColor,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("日志初始化失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logging.G().Debugf("日志已初始化 (级别=%s, 详细=%v, 静默=%v, 无色=%v)",
|
||||||
|
level, verbose, quiet, noColor)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,18 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 初始化日志
|
// 日志由 rootCmd.PersistentPreRunE 根据 CLI 参数自动初始化
|
||||||
logOpts := logging.Options{
|
// 程序退出时关闭日志文件句柄
|
||||||
Level: "info",
|
defer logging.G().Close()
|
||||||
LogFile: "devpack.log",
|
|
||||||
Verbose: false,
|
|
||||||
Quiet: false,
|
|
||||||
NoColor: false,
|
|
||||||
}
|
|
||||||
_, err := logging.Init(logOpts)
|
|
||||||
if err != nil {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := commands.Execute(); err != nil {
|
if err := commands.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user