support setting log by config file 35/58335/1
authorLvbo163 <lv.bo163@zte.com.cn>
Wed, 1 Aug 2018 06:05:34 +0000 (14:05 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Wed, 1 Aug 2018 06:05:34 +0000 (14:05 +0800)
Issue-ID: MSB-238

Change-Id: I8c1f5af356b430a1a24632b986545f01a67071b8
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/log/log.go

index 99c265f..8f1b4c4 100644 (file)
@@ -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{