reorganize log config
This commit is contained in:
parent
4919cdf7e7
commit
fa87b9293a
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
5
main.go
5
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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue