-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
134 lines (124 loc) · 3.19 KB
/
config.go
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
package log
import (
"bytes"
"encoding/json"
"github.com/op/go-logging"
"github.com/poolqa/log/rotator"
"os"
"strings"
)
const gDefaultConfigJson = `
# 備註說明:備註不能寫在後面,只能單行由#開頭
# Level : debug, info, notice, warn, error, critical(fatal)
# MaxSize : size * mb
# MaxAge : days
# Color : 只有在console才會生效
# DateSlice : d:天, h:小時
# Format :
# %{id} Sequence number for log message (uint64).
# %{pid} Process id (int)
# %{time} Time when log occurred (time.Time) ex:%{time:2006/01/02 15:04:05.000}
# %{level} Log level (Level)
# %{module} Module (string)
# %{program} Basename of os.Args[0] (string)
# %{message} Message (string)
# %{longfile} Full file name and line number: /a/b/c/d.go:23
# %{shortfile} Final file name element and line number: d.go:23
# %{callpath} Callpath like main.a.b.c...c "..." meaning recursive call ~. meaning truncated path
# %{color} ANSI color based on log level 不知道什麼時候才有效果
{
"Level": "DEBUG",
"FileName":"./logs/log.log",
"LevelFileName": {
# "error": "./logs/error.log"
},
"HasConsole": true,
"Color": true,
"MaxSize": 0,
"MaxAge": 0,
"DateSlice": "d",
"Format": "%{time:2006/01/02 15:04:05.000} %{shortfile} [%{level:.4s}] %{message}"
}
`
type LogConfig struct {
Level string
FileName string
LevelFileName map[string]string
HasConsole bool
Color bool
Json bool
MaxSize int
MaxAge int
DateSlice string
Format string
}
func LoadLogConfigFile(configJsonFilePath string) (*LogConfig, error) {
fp, err := os.Open(configJsonFilePath)
if err == nil {
defer fp.Close()
fileInfo, err := fp.Stat()
if err != nil {
return nil, err
}
buffer := make([]byte, fileInfo.Size())
_, err = fp.Read(buffer) // 文件内容读取到buffer中
if err != nil {
return nil, err
}
return LoadLogConfigJson(buffer)
}
return nil, err
}
func LoadLogConfigJson(configJson []byte) (*LogConfig, error) {
buffer := removeConfRemark(configJson)
config := LogConfig{}
err := json.Unmarshal(buffer, &config)
return &config, err
}
func GetDefaultLogConfig() []byte {
return []byte(gDefaultConfigJson)
}
func removeConfRemark(bConf []byte) []byte {
sConfLines := strings.Split(string(bConf), "\n")
buffer := bytes.Buffer{}
for _, line := range sConfLines {
newLine := strings.TrimSpace(line)
if len(newLine) == 0 || newLine[0] == '#' {
continue
}
buffer.WriteString(line + "\n")
}
out := make([]byte, buffer.Len())
_, _ = buffer.Read(out)
return out
}
func getLogLevel(strLv string) logging.Level {
switch strings.ToLower(strLv) {
case "debug":
return logging.DEBUG
case "info":
return logging.INFO
case "notice":
return logging.NOTICE
case "warn":
return logging.WARNING
case "error":
return logging.ERROR
case "fatal", "critical":
return logging.CRITICAL
default:
return logging.DEBUG
}
}
func getLogRotateMode(strMode string) rotator.RotateDateMode {
switch strings.ToLower(strMode) {
case "d":
return rotator.ROTATE_DATE_MODE_DAY
case "h":
return rotator.ROTATE_DATE_MODE_HOUR
case "m":
return rotator.ROTATE_DATE_MODE_MINUTE
default:
return rotator.ROTATE_DATE_MODE_NO
}
}