-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
133 lines (112 loc) · 3.76 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
package log4go
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
// GlobalLevel global level
var GlobalLevel = DEBUG
const (
WriterNameConsole = "console_writer"
WriterNameFile = "file_writer"
WriterNameKafka = "kafka_writer"
)
// LogConfig log config
type LogConfig struct {
Level string `json:"level" mapstructure:"level"`
Debug bool `json:"debug" mapstructure:"debug"` // output log info or not for log4go
FullPath bool `json:"full_path" mapstructure:"full_path"`
ConsoleWriter ConsoleWriterOptions `json:"console_writer" mapstructure:"console_writer"`
FileWriter FileWriterOptions `json:"file_writer" mapstructure:"file_writer"`
KafKaWriter KafKaWriterOptions `json:"kafka_writer" mapstructure:"kafka_writer"`
}
// SetupLog setup log
func SetupLog(lc LogConfig) (err error) {
if !lc.Debug {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
}
// global config
GlobalLevel = getLevel(lc.Level)
// writer enable
// 1. if not set level, use global level;
// 2. if set level, use min level
validGlobalMinLevel := EMERGENCY // default max level
validGlobalMinLevelBy := "global"
fileWriterLevelDefault := GlobalLevel
consoleWriterLevelDefault := GlobalLevel
kafkaWriterLevelDefault := GlobalLevel
if lc.ConsoleWriter.Enable {
consoleWriterLevelDefault = getLevelDefault(lc.ConsoleWriter.Level, GlobalLevel, WriterNameConsole)
validGlobalMinLevel = maxInt(consoleWriterLevelDefault, validGlobalMinLevel)
if validGlobalMinLevel == consoleWriterLevelDefault {
validGlobalMinLevelBy = WriterNameConsole
}
}
if lc.FileWriter.Enable {
fileWriterLevelDefault = getLevelDefault(lc.FileWriter.Level, GlobalLevel, WriterNameFile)
validGlobalMinLevel = maxInt(fileWriterLevelDefault, validGlobalMinLevel)
if validGlobalMinLevel == fileWriterLevelDefault {
validGlobalMinLevelBy = WriterNameFile
}
}
if lc.KafKaWriter.Enable {
kafkaWriterLevelDefault = getLevelDefault(lc.KafKaWriter.Level, GlobalLevel, WriterNameKafka)
validGlobalMinLevel = maxInt(kafkaWriterLevelDefault, validGlobalMinLevel)
if validGlobalMinLevel == kafkaWriterLevelDefault {
validGlobalMinLevelBy = WriterNameKafka
}
}
fullPath := lc.FullPath
WithFullPath(fullPath)
SetLevel(validGlobalMinLevel)
if lc.ConsoleWriter.Enable {
w := NewConsoleWriterWithOptions(lc.ConsoleWriter)
w.level = consoleWriterLevelDefault
log.Printf("[log4go] enable " + WriterNameConsole + " with level " + LevelFlags[consoleWriterLevelDefault])
Register(w)
}
if lc.FileWriter.Enable {
w := NewFileWriterWithOptions(lc.FileWriter)
w.level = fileWriterLevelDefault
log.Printf("[log4go] enable " + WriterNameFile + " with level " + LevelFlags[fileWriterLevelDefault])
Register(w)
}
if lc.KafKaWriter.Enable {
w := NewKafKaWriter(lc.KafKaWriter)
w.level = kafkaWriterLevelDefault
log.Printf("[log4go] enable " + WriterNameKafka + " with level " + LevelFlags[kafkaWriterLevelDefault])
Register(w)
}
log.Printf("[log4go] valid global_level(min:%v, flag:%v, by:%v), default(%v, flag:%v)",
validGlobalMinLevel, LevelFlags[validGlobalMinLevel], validGlobalMinLevelBy, GlobalLevel, LevelFlags[GlobalLevel])
return nil
}
// SetLogWithConf setup log with config file
func SetLogWithConf(file string) (err error) {
var lc LogConfig
cnt, err := ioutil.ReadFile(file)
if err = json.Unmarshal(cnt, &lc); err != nil {
return
}
return SetupLog(lc)
}
// SetLog setup log with config []byte
func SetLog(config []byte) (err error) {
var lc LogConfig
if err = json.Unmarshal(config, &lc); err != nil {
return
}
return SetupLog(lc)
}
func getLevel(flag string) int {
return getLevelDefault(flag, DEBUG, "")
}
// maxInt return max int
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}