init log util 85/58185/1
authorLvbo163 <lv.bo163@zte.com.cn>
Tue, 31 Jul 2018 07:44:11 +0000 (15:44 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Tue, 31 Jul 2018 07:44:11 +0000 (15:44 +0800)
init beego log util, define config parameters and init log by default setting.

Issue-ID: MSB-237

Change-Id: I203ecf1fc618162be5b244e37c8adcc9976f3cd6
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/log/log.go [new file with mode: 0644]
msb2pilot/src/msb2pilot/main.go

diff --git a/msb2pilot/src/msb2pilot/log/log.go b/msb2pilot/src/msb2pilot/log/log.go
new file mode 100644 (file)
index 0000000..99c265f
--- /dev/null
@@ -0,0 +1,114 @@
+/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and the Apache License 2.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ *     ZTE - initial Project
+ */
+
+package log
+
+import (
+       "encoding/json"
+       "fmt"
+       "os"
+       "strconv"
+       "strings"
+
+       "github.com/astaxie/beego/logs"
+)
+
+type ConsoleCfg struct {
+       Level int `json:"level"`
+}
+
+type FileCfg struct {
+       FileName string `json:"filename"`
+       Level    int    `json:"level"`
+       MaxLines int    `json:"maxlines"`
+       MaxSize  int    `josn:"maxsize"`
+       Daily    bool   `json:"daily"`
+       MaxDays  int    `json:"maxdays"`
+       Rotate   bool   `json:"rotate"`
+}
+
+type Cfg struct {
+       Console ConsoleCfg `json:"console"`
+       File    FileCfg    `json:"file"`
+}
+
+const (
+       cfgFileName         = "log.yml"
+       defaultConsoleLevel = "Warn"
+       defaultFileLevel    = "Info"
+)
+
+var (
+       Log         *logs.BeeLogger
+       loggerLevel = map[string]int{"Emergency": 0, "Alert": 1, "Critical": 2, "Error": 3, "Warn": 4, "Notice": 5, "Info": 6, "Debug": 7}
+)
+
+func init() {
+       Log = logs.NewLogger()
+       Log.EnableFuncCallDepth(true)
+
+       cfg := getDefaultCfg()
+       setLogger(logs.AdapterConsole, &cfg.Console)
+       checkLogDir(cfg.File.FileName)
+       setLogger(logs.AdapterFile, &cfg.File)
+}
+
+func setLogger(adapter string, cfg interface{}) bool {
+       b, err := json.Marshal(cfg)
+       if err != nil {
+               fmt.Printf(" cfg json trans error: %s\n", adapter, err.Error())
+               return false
+       }
+
+       err = Log.SetLogger(adapter, string(b))
+       if err != nil {
+               fmt.Printf("set %s failed: %s\n", adapter, err.Error())
+               return false
+       }
+
+       return true
+
+}
+
+func checkLogDir(path string) {
+       if path == "" {
+               return
+       }
+
+       var index int
+       pathSep := string(os.PathSeparator)
+       if index = strings.LastIndex(path, pathSep); index <= 2 {
+               return
+       }
+
+       perm, _ := strconv.ParseInt("0660", 8, 64)
+       if err := os.MkdirAll(path[0:index], os.FileMode(perm)); err != nil {
+               return
+       }
+}
+
+func getDefaultCfg() *Cfg {
+       return &Cfg{
+               Console: ConsoleCfg{
+                       Level: loggerLevel[defaultConsoleLevel],
+               },
+               File: FileCfg{
+                       FileName: "msb2pilot.log",
+                       Level:    loggerLevel[defaultFileLevel],
+                       MaxLines: 300000,
+                       MaxSize:  30 * 1024 * 1024,
+                       Daily:    true,
+                       MaxDays:  10,
+                       Rotate:   true,
+               },
+       }
+}
index 84e9b24..c19000a 100644 (file)
 package main
 
 import (
+       "msb2pilot/log"
        _ "msb2pilot/routers"
 
        "github.com/astaxie/beego"
 )
 
 func main() {
+       log.Log.Informational("**************** init msb2pilot ************************")
        beego.Run()
 }