...
1 package log
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7 )
8
9
10 const (
11 ckFormat = "LogFormat"
12 ckDefaultAdapterName = "LogDefaultAdapterName"
13 ckLevelName = "LogLevelName"
14 ckIncludeNouns = "LogIncludeNouns"
15 ckExcludeNouns = "LogExcludeNouns"
16 ckExcludeBypassLevelName = "LogExcludeBypassLevelName"
17 )
18
19
20 const (
21 defaultFormat = "{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}"
22 defaultLevelName = levelNameInfo
23 )
24
25
26 var (
27
28 format = defaultFormat
29
30
31 defaultAdapterName = ""
32
33
34 levelName = LogLevelName(strings.ToLower(string(defaultLevelName)))
35
36
37 includeNouns = ""
38
39
40 excludeNouns = ""
41
42
43
44 excludeBypassLevelName LogLevelName
45 )
46
47
48 var (
49 configurationLoaded = false
50 )
51
52
53 func GetDefaultAdapterName() string {
54 return defaultAdapterName
55 }
56
57
58
59 func SetDefaultAdapterName(name string) {
60 defaultAdapterName = name
61 }
62
63
64 func LoadConfiguration(cp ConfigurationProvider) {
65 configuredDefaultAdapterName := cp.DefaultAdapterName()
66
67 if configuredDefaultAdapterName != "" {
68 defaultAdapterName = configuredDefaultAdapterName
69 }
70
71 includeNouns = cp.IncludeNouns()
72 excludeNouns = cp.ExcludeNouns()
73 excludeBypassLevelName = cp.ExcludeBypassLevelName()
74
75 f := cp.Format()
76 if f != "" {
77 format = f
78 }
79
80 ln := cp.LevelName()
81 if ln != "" {
82 levelName = LogLevelName(strings.ToLower(string(ln)))
83 }
84
85 configurationLoaded = true
86 }
87
88 func getConfigState() map[string]interface{} {
89 return map[string]interface{}{
90 "format": format,
91 "defaultAdapterName": defaultAdapterName,
92 "levelName": levelName,
93 "includeNouns": includeNouns,
94 "excludeNouns": excludeNouns,
95 "excludeBypassLevelName": excludeBypassLevelName,
96 }
97 }
98
99 func setConfigState(config map[string]interface{}) {
100 format = config["format"].(string)
101
102 defaultAdapterName = config["defaultAdapterName"].(string)
103
104 levelName = config["levelName"].(LogLevelName)
105 levelName = LogLevelName(strings.ToLower(string(levelName)))
106
107 includeNouns = config["includeNouns"].(string)
108 excludeNouns = config["excludeNouns"].(string)
109 excludeBypassLevelName = config["excludeBypassLevelName"].(LogLevelName)
110 }
111
112 func getConfigDump() string {
113 return fmt.Sprintf(
114 "Current configuration:\n"+
115 " FORMAT=[%s]\n"+
116 " DEFAULT-ADAPTER-NAME=[%s]\n"+
117 " LEVEL-NAME=[%s]\n"+
118 " INCLUDE-NOUNS=[%s]\n"+
119 " EXCLUDE-NOUNS=[%s]\n"+
120 " EXCLUDE-BYPASS-LEVEL-NAME=[%s]",
121 format, defaultAdapterName, levelName, includeNouns, excludeNouns, excludeBypassLevelName)
122 }
123
124
125 func IsConfigurationLoaded() bool {
126 return configurationLoaded
127 }
128
129
130 type ConfigurationProvider interface {
131
132 Format() string
133
134
135 DefaultAdapterName() string
136
137
138
139 LevelName() LogLevelName
140
141
142
143 IncludeNouns() string
144
145
146
147 ExcludeNouns() string
148
149
150
151 ExcludeBypassLevelName() LogLevelName
152 }
153
154
155 type EnvironmentConfigurationProvider struct {
156 }
157
158
159
160 func NewEnvironmentConfigurationProvider() *EnvironmentConfigurationProvider {
161 return new(EnvironmentConfigurationProvider)
162 }
163
164
165 func (ecp *EnvironmentConfigurationProvider) Format() string {
166 return os.Getenv(ckFormat)
167 }
168
169
170 func (ecp *EnvironmentConfigurationProvider) DefaultAdapterName() string {
171 return os.Getenv(ckDefaultAdapterName)
172 }
173
174
175 func (ecp *EnvironmentConfigurationProvider) LevelName() LogLevelName {
176 return LogLevelName(os.Getenv(ckLevelName))
177 }
178
179
180 func (ecp *EnvironmentConfigurationProvider) IncludeNouns() string {
181 return os.Getenv(ckIncludeNouns)
182 }
183
184
185 func (ecp *EnvironmentConfigurationProvider) ExcludeNouns() string {
186 return os.Getenv(ckExcludeNouns)
187 }
188
189
190
191 func (ecp *EnvironmentConfigurationProvider) ExcludeBypassLevelName() LogLevelName {
192 return LogLevelName(os.Getenv(ckExcludeBypassLevelName))
193 }
194
195
196 type StaticConfigurationProvider struct {
197 format string
198 defaultAdapterName string
199 levelName LogLevelName
200 includeNouns string
201 excludeNouns string
202 excludeBypassLevelName LogLevelName
203 }
204
205
206
207 func NewStaticConfigurationProvider() *StaticConfigurationProvider {
208 return new(StaticConfigurationProvider)
209 }
210
211
212 func (scp *StaticConfigurationProvider) SetFormat(format string) {
213 scp.format = format
214 }
215
216
217 func (scp *StaticConfigurationProvider) SetDefaultAdapterName(adapterName string) {
218 scp.defaultAdapterName = adapterName
219 }
220
221
222 func (scp *StaticConfigurationProvider) SetLevelName(levelName LogLevelName) {
223 scp.levelName = LogLevelName(strings.ToLower(string(levelName)))
224 }
225
226
227 func (scp *StaticConfigurationProvider) SetLevel(level LogLevel) {
228 scp.levelName = levelNameMapR[level]
229 }
230
231
232 func (scp *StaticConfigurationProvider) SetIncludeNouns(includeNouns string) {
233 scp.includeNouns = includeNouns
234 }
235
236
237 func (scp *StaticConfigurationProvider) SetExcludeNouns(excludeNouns string) {
238 scp.excludeNouns = excludeNouns
239 }
240
241
242
243 func (scp *StaticConfigurationProvider) SetExcludeBypassLevelName(excludeBypassLevelName LogLevelName) {
244 scp.excludeBypassLevelName = excludeBypassLevelName
245 }
246
247
248 func (scp *StaticConfigurationProvider) Format() string {
249 return scp.format
250 }
251
252
253 func (scp *StaticConfigurationProvider) DefaultAdapterName() string {
254 return scp.defaultAdapterName
255 }
256
257
258 func (scp *StaticConfigurationProvider) LevelName() LogLevelName {
259 return scp.levelName
260 }
261
262
263 func (scp *StaticConfigurationProvider) IncludeNouns() string {
264 return scp.includeNouns
265 }
266
267
268 func (scp *StaticConfigurationProvider) ExcludeNouns() string {
269 return scp.excludeNouns
270 }
271
272
273
274 func (scp *StaticConfigurationProvider) ExcludeBypassLevelName() LogLevelName {
275 return scp.excludeBypassLevelName
276 }
277
278 func init() {
279
280
281 ecp := NewEnvironmentConfigurationProvider()
282 LoadConfiguration(ecp)
283 }
284
View as plain text