项目目录结构
demo
config
config.go
conf
config.yaml
log
main.go
代码
config.go
package config import ( "fmt" "github.com/toolkits/pkg/file" "github.com/toolkits/pkg/logger" "os" ) type ConfigT struct { Logger Logger `yaml:"logger"` Info Info `yaml:"info"` } type Logger struct { Level string `yaml:"level"` Dir string `yaml:"dir"` KeepHours uint `yaml:"keepHours"` } type Info struct { Name string `yaml:"name"` Age int `yaml:"age"` } var Config *ConfigT func Init(c *ConfigT) { fb, err := logger.NewFileBackend(c.Logger.Dir) if err != nil { fmt.Println("cannot init logger:", err) os.Exit(1) } fb.SetRotateByHour(false) // 关闭按小时滚动 fb.SetKeepHours(c.Logger.KeepHours) // 保留多少小时 logger.SetLogging(c.Logger.Level, fb) // 设置日志级别 } func GetConfig() *ConfigT { err := file.ReadYaml("conf/config.yaml", &Config) if err != nil { panic("读取配置config.yaml失败") } return Config }
config.yaml
logger: level: "DEBG" info: name: "tom" age: 23
标签:err,ConfigT,golang,yaml,开发,Logger,config,logger From: https://www.cnblogs.com/fighting-ljy/p/18198145