代码拉取完成,页面将自动刷新
package config
import (
"strings"
"gitee.com/lyhuilin/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Config struct {
Name string
}
func Init(cfg string) error {
c := Config{
Name: cfg,
}
//初始化配置文件
if err := c.initConfig(); err != nil {
return err
}
//初始化日志包
c.initLog()
//监控配置文件变化并热加载程序
c.watchConfig()
return nil
}
func (c *Config) initConfig() error {
if c.Name != "" {
viper.SetConfigFile(c.Name) //如果指定了配置文件,则解析指定的配置文件
} else {
viper.AddConfigPath("conf")
viper.SetConfigName("config")
}
viper.SetConfigType("yaml") //设置配置文件格式为YAML
viper.AutomaticEnv() //读取匹配的环境变量
viper.SetEnvPrefix("HLTYAPI") //读取环境变量的前缀为GOSPIDER
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil { //viper 解析配置文件
return err
}
return nil
}
// 日志配置加载
func (c *Config) initLog() {
passLagerCfg := log.PassLagerCfg{
Writers: viper.GetString("log.writers"),
LoggerLevel: viper.GetString("log.logger_level"),
LoggerFile: viper.GetString("log.logger_file"),
LogFormatText: viper.GetBool("log.log_format_text"),
RollingPolicy: viper.GetString("log.rollingPolicy"),
LogRotateDate: viper.GetInt("log.log_rotate_date"),
LogRotateSize: viper.GetInt("log.log_rotate_size"),
LogBackupCount: viper.GetInt("log.log_backup_count"),
}
log.InitWithConfig(&passLagerCfg)
}
//监控配置文件变化并热加载程序
func (c *Config) watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Infof("配置发生变更:%s", e.Name)
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。