Beego-logs+viper实现logs日志记录配置文件热部署
1.官方中文文档:[beego-logs]https://beego.me/docs/module/logs.md [viper]https://github.com/spf13/viper?files=1
-
2.配置文件(config/logs.json)
{ "logs_level":7, "logs_func_call": true, "logs_Adapter": "console", "logs_Adapter_config": "{\"level\":{LogsLevel}}" }
-
3.model
package model import ( "fmt" "github.com/astaxie/beego/logs" "github.com/fsnotify/fsnotify" "github.com/mitchellh/mapstructure" "github.com/spf13/viper" "strconv" "strings" ) type LogsConfig struct { LogsLevel int `json:"logs_level"` //日志级别 LogsFuncCall bool `json:"logs_func_call"` //日志输出调用的文件名和文件行号 LogsAdapter string `json:"logs_adapter"` //日志输出方式 LogsAdapterConfig string `json:"logs_adapter_config"` //日志输出配置 } var WebLogsConfig LogsConfig var webLogsConfigPath = "config/logs.json" // 加载配置文件 func init() { v := viper.New() logs.Informational("package model,logs_config init()...") // 加载配置文件 WebLogsConfig = LogsConfig{ LogsLevel: 3, LogsFuncCall: false, } v.SetConfigFile(webLogsConfigPath) //直接读取配置文件 errByRead := v.ReadInConfig() if errByRead != nil { logs.Error("package model,logs_config init() viper read config error......%s", errByRead.Error()) } errByUms := v.Unmarshal(&WebLogsConfig, func(config *mapstructure.DecoderConfig) { //设置解析对应标签 config.TagName = "json" }) if errByUms != nil { logs.Error("package model,logs_config init() viper unmarshal config to struct error......%s", errByUms.Error()) } //监听配置文件改变 v.WatchConfig() v.OnConfigChange(func(in fsnotify.Event) { fmt.Println("前:", WebLogsConfig.LogsLevel) fmt.Println("Config file changed:", in.Name) //配置文件改变 //重新解析 errByRead := v.ReadInConfig() if errByRead != nil { logs.Error("package model,logs_config init() viper read config error......%s", errByRead.Error()) } errByUms := v.Unmarshal(&WebLogsConfig, func(config *mapstructure.DecoderConfig) { //设置解析对应标签 config.TagName = "json" }) fmt.Println("后:", WebLogsConfig.LogsLevel) if errByUms != nil { logs.Error("package model,logs_config init() viper unmarshal config to struct error......%s", errByUms.Error()) } //初始化配置 InitLog() }) //初始化配置 InitLog() } //初始化日志配置 func InitLog() { //出现error:logs: duplicate adaptername "console" (you have set this logger before) //配置文件修改记录器会导致多个记录器同时存在 //配置日志记录器前需要删除日志记录器 //重新设置记录器 logs.Reset() //删除指定记录器 //errByDelLogger := logs.GetBeeLogger().DelLogger(WebLogsConfig.LogsAdapter) //if errByDelLogger != nil { // logs.Error("package model,logs_config InitLog() logs del logger config error......%s", errByDelLogger.Error()) //} //设置日志级别 logs.SetLevel(WebLogsConfig.LogsLevel) //设置记录器 LogsAdapterConfig := strings.Replace(WebLogsConfig.LogsAdapterConfig, "{LogsLevel}", strconv.Itoa(WebLogsConfig.LogsLevel), -1) errByLogsConfig := logs.SetLogger(WebLogsConfig.LogsAdapter, LogsAdapterConfig) if errByLogsConfig != nil { logs.Error("package model,logs_config InitLog() logs config error......%s", errByLogsConfig.Error()) } //设置是否输出调用的文件名和文件行号 logs.SetLogFuncCall(WebLogsConfig.LogsFuncCall) }
-
5.使用
package main import ( "eastwan.com/eascs-eatp/model" ) func main() { fmt.Printf("日志级别:%v \n",model.WebLogsConfig.LogsLevel) logs.Emergency("This is Emergency......") logs.Alert("This is Alert......") logs.Critical("This is Critical......") logs.Error("This is Error......") logs.Warning("This is Warning......") logs.Notice("This is Notice......") logs.Info("This is Info......") logs.Informational("This is Informational......") logs.Debug("This is Debug......") }