diff options
author | Huabing Zhao <zhaohuabing@gmail.com> | 2018-07-31 07:59:21 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-07-31 07:59:21 +0000 |
commit | f8249803e051f84d04b8de65358663efc719ec45 (patch) | |
tree | 2cb60ca90643b8a2c795e45f5723faba775d5b79 | |
parent | f2d7ee8e726abb9e69b6ed0416d2493eac74a2b3 (diff) | |
parent | dba75a52f7b929683446c549fd7843624fd1e080 (diff) |
Merge "init log util"
-rw-r--r-- | msb2pilot/src/msb2pilot/log/log.go | 114 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/main.go | 2 |
2 files changed, 116 insertions, 0 deletions
diff --git a/msb2pilot/src/msb2pilot/log/log.go b/msb2pilot/src/msb2pilot/log/log.go new file mode 100644 index 0000000..99c265f --- /dev/null +++ b/msb2pilot/src/msb2pilot/log/log.go @@ -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, + }, + } +} diff --git a/msb2pilot/src/msb2pilot/main.go b/msb2pilot/src/msb2pilot/main.go index 84e9b24..c19000a 100644 --- a/msb2pilot/src/msb2pilot/main.go +++ b/msb2pilot/src/msb2pilot/main.go @@ -12,11 +12,13 @@ package main import ( + "msb2pilot/log" _ "msb2pilot/routers" "github.com/astaxie/beego" ) func main() { + log.Log.Informational("**************** init msb2pilot ************************") beego.Run() } |