From fa87b9293a373cc97b0a41e198f5565f5ef9a7a7 Mon Sep 17 00:00:00 2001 From: kongfei Date: Thu, 2 Jun 2022 19:29:49 +0800 Subject: [PATCH] reorganize log config --- config/config.go | 1 + config/logs.go | 33 --------------------------------- main.go | 5 +---- pkg/cfg/cfg.go | 28 ---------------------------- 4 files changed, 2 insertions(+), 65 deletions(-) diff --git a/config/config.go b/config/config.go index be10ebd..0438715 100644 --- a/config/config.go +++ b/config/config.go @@ -47,6 +47,7 @@ type ConfigType struct { Global Global `toml:"global"` WriterOpt WriterOpt `toml:"writer_opt"` Writers []WriterOption `toml:"writers"` + Logs Logs `toml:"logs"` } var Config *ConfigType diff --git a/config/logs.go b/config/logs.go index 4fcc8c0..bf696e1 100644 --- a/config/logs.go +++ b/config/logs.go @@ -1,14 +1,7 @@ package config import ( - "encoding/json" - "fmt" - "path" - - "github.com/toolkits/pkg/file" - logsconfig "flashcat.cloud/categraf/config/logs" - "flashcat.cloud/categraf/pkg/cfg" ) const ( @@ -34,38 +27,12 @@ type ( GlobalProcessingRules []*logsconfig.ProcessingRule `json:"processing_rules" toml:"processing_rules"` Items []*logsconfig.LogsConfig `json:"items" toml:"items"` } - LogType struct { - Logs *Logs `json:"logs" toml:"logs"` - } ) var ( LogConfig *Logs ) -func InitLogConfig(configDir string) error { - var ( - err error - ) - configFile := path.Join(configDir, "logs.toml") - if !file.IsExist(configFile) { - return fmt.Errorf("configuration file(%s) not found", configFile) - } - data := &LogType{} - err = cfg.LoadConfig(configFile, data) - if err != nil { - return fmt.Errorf("failed to load config: %s, err: %s", configFile, err) - } - - LogConfig = data.Logs - if Config != nil && Config.Global.PrintConfigs { - bs, _ := json.MarshalIndent(LogConfig, "", " ") - fmt.Println(string(bs)) - } - - return nil -} - func GetLogRunPath() string { if len(LogConfig.RunPath) == 0 { LogConfig.RunPath = "/opt/categraf/run" diff --git a/main.go b/main.go index 0770618..1f06d6e 100644 --- a/main.go +++ b/main.go @@ -61,10 +61,7 @@ func main() { if err := config.InitConfig(*configDir, *debugMode, *testMode); err != nil { log.Fatalln("F! failed to init config:", err) } - // init log config - if err := config.InitLogConfig(*configDir); err != nil { - log.Fatalln("F! failed to init config:", err) - } + // init writers if err := writer.Init(config.Config.Writers); err != nil { log.Fatalln("F! failed to init writer:", err) diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go index 1f50f58..4424d39 100644 --- a/pkg/cfg/cfg.go +++ b/pkg/cfg/cfg.go @@ -21,10 +21,6 @@ func LoadConfigs(configDir string, configPtr interface{}) error { } for _, fpath := range files { - // logs.toml 单独解析 - if fpath == "logs.toml" || fpath == "logs.yaml" || fpath == "logs.json" { - continue - } if strings.HasSuffix(fpath, "toml") { loaders = append(loaders, &multiconfig.TOMLLoader{Path: path.Join(configDir, fpath)}) } @@ -43,27 +39,3 @@ func LoadConfigs(configDir string, configPtr interface{}) error { return m.Load(configPtr) } - -func LoadConfig(configFile string, configPtr interface{}) error { - loaders := []multiconfig.Loader{ - &multiconfig.TagLoader{}, - &multiconfig.EnvironmentLoader{}, - } - - if strings.HasSuffix(configFile, "toml") { - loaders = append(loaders, &multiconfig.TOMLLoader{Path: configFile}) - } - if strings.HasSuffix(configFile, "json") { - loaders = append(loaders, &multiconfig.JSONLoader{Path: configFile}) - } - if strings.HasSuffix(configFile, "yaml") || strings.HasSuffix(configFile, "yml") { - loaders = append(loaders, &multiconfig.YAMLLoader{Path: configFile}) - } - - m := multiconfig.DefaultLoader{ - Loader: multiconfig.MultiLoader(loaders...), - Validator: multiconfig.MultiValidator(&multiconfig.RequiredValidator{}), - } - - return m.Load(configPtr) -}