diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-01 14:05:34 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-01 14:05:34 +0800 |
commit | 6a30d22008bf4e7e33f876087e368810486bd59e (patch) | |
tree | 2a8dc30bdd167aa9817e0f4e2b33659381998295 /msb2pilot | |
parent | 45198f48b37f69c1b4764f93f423c5ceeaece06a (diff) |
support setting log by config file
Issue-ID: MSB-238
Change-Id: I8c1f5af356b430a1a24632b986545f01a67071b8
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot')
-rw-r--r-- | msb2pilot/src/msb2pilot/log/log.go | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/msb2pilot/src/msb2pilot/log/log.go b/msb2pilot/src/msb2pilot/log/log.go index 99c265f..8f1b4c4 100644 --- a/msb2pilot/src/msb2pilot/log/log.go +++ b/msb2pilot/src/msb2pilot/log/log.go @@ -56,7 +56,7 @@ func init() { Log = logs.NewLogger() Log.EnableFuncCallDepth(true) - cfg := getDefaultCfg() + cfg := getConfig() setLogger(logs.AdapterConsole, &cfg.Console) checkLogDir(cfg.File.FileName) setLogger(logs.AdapterFile, &cfg.File) @@ -96,6 +96,82 @@ func checkLogDir(path string) { } } +func loadCustom() map[string]interface{} { + fullPath := filepath.Join("", cfgFileName) + fmt.Println("log config path is:" + fullPath) + config, err := util.Read(fullPath) + if err != nil { + fmt.Println("read config file error") + return nil + } + + cfg := make(map[string]interface{}) + err = util.UnmarshalYaml(config, &cfg) + + if err != nil { + fmt.Printf("parse config file error: %s\n", err.Error()) + return nil + } + return cfg +} + +func getConfig() (result *Cfg) { + result = getDefaultCfg() + + customs := loadCustom() + + if customs == nil { + return + } + + var console map[interface{}]interface{} + if cons, exist := customs["console"]; exist { + console = cons.(map[interface{}]interface{}) + + if levelstr, exist := console["level"]; exist { + if level, ok := loggerLevel[levelstr.(string)]; ok { + result.Console.Level = level + } + } + } + var file map[interface{}]interface{} + if f, exist := customs["file"]; !exist { + return + } else { + file = f.(map[interface{}]interface{}) + } + + if filename, e := file["filename"]; e { + result.File.FileName = filename.(string) + } + if levelstr, e := file["level"]; e { + if level, exist := loggerLevel[levelstr.(string)]; exist { + result.File.Level = level + } + } + if maxlines, e := file["maxlines"]; e { + result.File.MaxLines = maxlines.(int) + } + + if maxsize, e := file["maxsize"]; e { + result.File.MaxSize = maxsize.(int) * 1024 * 1024 + } + + if daily, e := file["daily"]; e { + result.File.Daily = daily.(bool) + } + + if maxdays, e := file["maxdays"]; e { + result.File.MaxDays = maxdays.(int) + } + + if rotate, e := file["rotate"]; e { + result.File.Rotate = rotate.(bool) + } + + return +} + func getDefaultCfg() *Cfg { return &Cfg{ Console: ConsoleCfg{ |