summaryrefslogtreecommitdiff
path: root/vendor/github.com/dsoprea/go-logging/config.go
blob: 20896e342b93c513e8f2d14d595cab58614c28ad (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package log

import (
	"fmt"
	"os"
)

// Config keys.
const (
	ckFormat                 = "LogFormat"
	ckDefaultAdapterName     = "LogDefaultAdapterName"
	ckLevelName              = "LogLevelName"
	ckIncludeNouns           = "LogIncludeNouns"
	ckExcludeNouns           = "LogExcludeNouns"
	ckExcludeBypassLevelName = "LogExcludeBypassLevelName"
)

// Other constants
const (
	defaultFormat    = "{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}"
	defaultLevelName = LevelNameInfo
)

// Config
var (
	// Alternative format.
	format = defaultFormat

	// Alternative adapter.
	defaultAdapterName = ""

	// Alternative level at which to display log-items
	levelName = defaultLevelName

	// Configuration-driven comma-separated list of nouns to include.
	includeNouns = ""

	// Configuration-driven comma-separated list of nouns to exclude.
	excludeNouns = ""

	// Level at which to disregard exclusion (if the severity of a message
	// meets or exceed this, always display).
	excludeBypassLevelName = ""
)

// Other
var (
	configurationLoaded = false
)

// Return the current default adapter name.
func GetDefaultAdapterName() string {
	return defaultAdapterName
}

// The adapter will automatically be the first one registered. This overrides
// that.
func SetDefaultAdapterName(name string) {
	defaultAdapterName = name
}

func LoadConfiguration(cp ConfigurationProvider) {
	configuredDefaultAdapterName := cp.DefaultAdapterName()

	if configuredDefaultAdapterName != "" {
		defaultAdapterName = configuredDefaultAdapterName
	}

	includeNouns = cp.IncludeNouns()
	excludeNouns = cp.ExcludeNouns()
	excludeBypassLevelName = cp.ExcludeBypassLevelName()

	f := cp.Format()
	if f != "" {
		format = f
	}

	ln := cp.LevelName()
	if ln != "" {
		levelName = ln
	}

	configurationLoaded = true
}

func getConfigState() map[string]interface{} {
	return map[string]interface{}{
		"format":                 format,
		"defaultAdapterName":     defaultAdapterName,
		"levelName":              levelName,
		"includeNouns":           includeNouns,
		"excludeNouns":           excludeNouns,
		"excludeBypassLevelName": excludeBypassLevelName,
	}
}

func setConfigState(config map[string]interface{}) {
	format = config["format"].(string)

	defaultAdapterName = config["defaultAdapterName"].(string)
	levelName = config["levelName"].(string)
	includeNouns = config["includeNouns"].(string)
	excludeNouns = config["excludeNouns"].(string)
	excludeBypassLevelName = config["excludeBypassLevelName"].(string)
}

func getConfigDump() string {
	return fmt.Sprintf(
		"Current configuration:\n"+
			"  FORMAT=[%s]\n"+
			"  DEFAULT-ADAPTER-NAME=[%s]\n"+
			"  LEVEL-NAME=[%s]\n"+
			"  INCLUDE-NOUNS=[%s]\n"+
			"  EXCLUDE-NOUNS=[%s]\n"+
			"  EXCLUDE-BYPASS-LEVEL-NAME=[%s]",
		format, defaultAdapterName, levelName, includeNouns, excludeNouns, excludeBypassLevelName)
}

func IsConfigurationLoaded() bool {
	return configurationLoaded
}

type ConfigurationProvider interface {
	// Alternative format (defaults to .
	Format() string

	// Alternative adapter (defaults to "appengine").
	DefaultAdapterName() string

	// Alternative level at which to display log-items (defaults to
	// "info").
	LevelName() string

	// Configuration-driven comma-separated list of nouns to include. Defaults
	// to empty.
	IncludeNouns() string

	// Configuration-driven comma-separated list of nouns to exclude. Defaults
	// to empty.
	ExcludeNouns() string

	// Level at which to disregard exclusion (if the severity of a message
	// meets or exceed this, always display). Defaults to empty.
	ExcludeBypassLevelName() string
}

// Environment configuration-provider.
type EnvironmentConfigurationProvider struct {
}

func NewEnvironmentConfigurationProvider() *EnvironmentConfigurationProvider {
	return new(EnvironmentConfigurationProvider)
}

func (ecp *EnvironmentConfigurationProvider) Format() string {
	return os.Getenv(ckFormat)
}

func (ecp *EnvironmentConfigurationProvider) DefaultAdapterName() string {
	return os.Getenv(ckDefaultAdapterName)
}

func (ecp *EnvironmentConfigurationProvider) LevelName() string {
	return os.Getenv(ckLevelName)
}

func (ecp *EnvironmentConfigurationProvider) IncludeNouns() string {
	return os.Getenv(ckIncludeNouns)
}

func (ecp *EnvironmentConfigurationProvider) ExcludeNouns() string {
	return os.Getenv(ckExcludeNouns)
}

func (ecp *EnvironmentConfigurationProvider) ExcludeBypassLevelName() string {
	return os.Getenv(ckExcludeBypassLevelName)
}

// Static configuration-provider.
type StaticConfigurationProvider struct {
	format                 string
	defaultAdapterName     string
	levelName              string
	includeNouns           string
	excludeNouns           string
	excludeBypassLevelName string
}

func NewStaticConfigurationProvider() *StaticConfigurationProvider {
	return new(StaticConfigurationProvider)
}

func (scp *StaticConfigurationProvider) SetFormat(format string) {
	scp.format = format
}

func (scp *StaticConfigurationProvider) SetDefaultAdapterName(adapterName string) {
	scp.defaultAdapterName = adapterName
}

func (scp *StaticConfigurationProvider) SetLevelName(levelName string) {
	scp.levelName = levelName
}

func (scp *StaticConfigurationProvider) SetIncludeNouns(includeNouns string) {
	scp.includeNouns = includeNouns
}

func (scp *StaticConfigurationProvider) SetExcludeNouns(excludeNouns string) {
	scp.excludeNouns = excludeNouns
}

func (scp *StaticConfigurationProvider) SetExcludeBypassLevelName(excludeBypassLevelName string) {
	scp.excludeBypassLevelName = excludeBypassLevelName
}

func (scp *StaticConfigurationProvider) Format() string {
	return scp.format
}

func (scp *StaticConfigurationProvider) DefaultAdapterName() string {
	return scp.defaultAdapterName
}

func (scp *StaticConfigurationProvider) LevelName() string {
	return scp.levelName
}

func (scp *StaticConfigurationProvider) IncludeNouns() string {
	return scp.includeNouns
}

func (scp *StaticConfigurationProvider) ExcludeNouns() string {
	return scp.excludeNouns
}

func (scp *StaticConfigurationProvider) ExcludeBypassLevelName() string {
	return scp.excludeBypassLevelName
}

func init() {
	// Do the initial configuration-load from the environment. We gotta seed it
	// with something for simplicity's sake.
	ecp := NewEnvironmentConfigurationProvider()
	LoadConfiguration(ecp)
}