summaryrefslogtreecommitdiffstats
path: root/msb2pilot2/log/log.go
blob: 253d8b252c650a4da2022448b7e1ff23c919dbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package log

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/logs"
	"msb2pilot/util"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

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 := getConfig()
	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 loadCustom() map[string]interface{} {
	fullPath := filepath.Join(util.GetCfgPath(), 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{
			Level: loggerLevel[defaultConsoleLevel],
		},
		File: FileCfg{
			FileName: "msb2pilot.log",
			Level:    loggerLevel[defaultFileLevel],
			MaxLines: 300000,
			MaxSize:  30 * 1024 * 1024,
			Daily:    true,
			MaxDays:  10,
			Rotate:   true,
		},
	}
}