This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
141 lines (121 loc) · 4.25 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
135
136
137
138
139
140
141
package log4go
import (
"encoding/json"
"io/ioutil"
"log"
"strings"
"github.com/imb2022/log4go/util"
)
// GlobalLevel global level
var GlobalLevel = DEBUG
// ConfFileWriter file writer config
type ConfFileWriter struct {
Level string `json:"level" mapstructure:"level"`
LogPath string `json:"log_path" mapstructure:"log_path"`
Enable bool `json:"enable" mapstructure:"enable"`
}
// ConfConsoleWriter console writer config
type ConfConsoleWriter struct {
Level string `json:"level" mapstructure:"level"`
Enable bool `json:"enable" mapstructure:"enable"`
Color bool `json:"color" mapstructure:"color"`
}
// ConfAliLogHubWriter ali log hub writer config
type ConfAliLogHubWriter struct {
Level string `json:"level" mapstructure:"level"`
Enable bool `json:"enable" mapstructure:"enable"`
LogName string `json:"log_name" mapstructure:"log_name"`
LogSource string `json:"log_source" mapstructure:"log_source"`
ProjectName string `json:"project_name" mapstructure:"project_name"`
Endpoint string `json:"endpoint" mapstructure:"endpoint"`
AccessKeyId string `json:"access_key_id" mapstructure:"access_key_id"`
AccessKeySecret string `json:"access_key_secret" mapstructure:"access_key_secret"`
StoreName string `json:"store_name" mapstructure:"store_name"`
BufSize int `json:"buf_size" mapstructure:"buf_size"`
}
// LogConfig log config
type LogConfig struct {
// global level, maybe override by real minimum multi writer level
Level string `json:"level" mapstructure:"level"`
FullPath bool `json:"full_path" mapstructure:"full_path"`
FileWriter ConfFileWriter `json:"file_writer" mapstructure:"file_writer"`
ConsoleWriter ConfConsoleWriter `json:"console_writer" mapstructure:"console_writer"`
KafKaWriter ConfKafKaWriter `json:"kafka_writer" mapstructure:"kafka_writer"`
}
// SetupLog setup log, caller shall use this method
func SetupLog(lc LogConfig) (err error) {
// global config
GlobalLevel = getLevel(lc.Level)
// writer enable
// 1. if not set level, use global level;
// 2. if set level, use min level
validGlobalMinLevel := FATAL // default max level
validGlobalMinLevelBy := "global"
if lc.FileWriter.Enable {
validGlobalMinLevel = util.MinInt(getLevel0(lc.FileWriter.Level, GlobalLevel), validGlobalMinLevel)
if validGlobalMinLevel == getLevel0(lc.FileWriter.Level, GlobalLevel) {
validGlobalMinLevelBy = "file_writer"
}
}
if lc.ConsoleWriter.Enable {
validGlobalMinLevel = util.MinInt(getLevel0(lc.ConsoleWriter.Level, GlobalLevel), validGlobalMinLevel)
if validGlobalMinLevel == getLevel0(lc.ConsoleWriter.Level, GlobalLevel) {
validGlobalMinLevelBy = "console_writer"
}
}
if lc.KafKaWriter.Enable {
validGlobalMinLevel = util.MinInt(getLevel0(lc.KafKaWriter.Level, GlobalLevel), validGlobalMinLevel)
if validGlobalMinLevel == getLevel0(lc.KafKaWriter.Level, GlobalLevel) {
validGlobalMinLevelBy = "kafka_writer"
}
}
fullPath := lc.FullPath
ShowFullPath(fullPath)
SetLevel(validGlobalMinLevel)
if lc.FileWriter.Enable {
w := NewFileWriter()
w.level = getLevel0(lc.FileWriter.Level, GlobalLevel)
if err = w.SetPathPattern(lc.FileWriter.LogPath); err != nil {
return err
}
Register(w)
}
if lc.ConsoleWriter.Enable {
w := NewConsoleWriter()
w.level = getLevel0(lc.ConsoleWriter.Level, GlobalLevel)
w.SetColor(lc.ConsoleWriter.Color)
Register(w)
}
if lc.KafKaWriter.Enable {
w := NewKafKaWriter(&lc.KafKaWriter)
w.level = getLevel0(lc.KafKaWriter.Level, GlobalLevel)
Register(w)
}
log.Printf("validGlobalLevel(min:%v, by:%v)", validGlobalMinLevel, validGlobalMinLevelBy)
return nil
}
// SetupLogWithConf setup log with config file
func SetupLogWithConf(file string) (err error) {
var lc LogConfig
cnt, err := ioutil.ReadFile(file)
if err != nil {
return
}
if err = json.Unmarshal(cnt, &lc); err != nil {
return
}
return SetupLog(lc)
}
func getLevel(flag string) int {
return getLevel0(flag, DEBUG)
}
// default DEBUG
func getLevel0(flag string, defaultFlag int) int {
for i, f := range LevelFlags {
if strings.TrimSpace(strings.ToUpper(flag)) == f {
return i
}
}
log.Printf("[log4go] WARN, no match level for: %v, use default level: %d\n", flag, defaultFlag)
return defaultFlag
}