/
config.go
194 lines (162 loc) · 4.77 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
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
package log
import (
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/go-errors/errors"
"github.com/rs/zerolog"
)
const (
AppVerDefault = "1.0.0"
AwsAccountIDDefault = "development"
AppFarmDefault = "local"
LogLevelDefault = "INFO"
)
type timeNowFunc = func() time.Time
// Config contains logging configuration values.
type Config struct {
// Mandatory fields listed in https://cultureamp.atlassian.net/wiki/spaces/TV/pages/3114598406/Logging+Standard
AppName string // The name of the application the log was generated from
AppVersion string // The version of the application
AwsRegion string // the AWS region this code is running in
AwsAccountID string // the AWS account ID this code is running in
Product string // performance, engagmentment, etc.
Farm string // The name of the farm or where the code is running
LogLevel string // The logging level
Quiet bool // Are we running inside tests and we should be quiet?
ConsoleWriter bool // If Farm=local use key-value colour console logging
ConsoleColour bool // If ConsoleWriter=true then enable/disable colour
TimeNow timeNowFunc // Defaults to "time.Now" but useful to set in tests
}
// NewLoggerConfig creates a new configuration based on environment variables
// which can easily be reset before passing to NewLogger().
func NewLoggerConfig() *Config {
appName := os.Getenv(AppNameEnv)
if appName == "" {
appName = os.Getenv(AppNameLeagcyEnv)
}
appVersion := os.Getenv(AppVerEnv)
if appVersion == "" {
appVersion = AppVerDefault
}
awsRegion := os.Getenv(AwsRegionEnv)
product := os.Getenv(ProductEnv)
awsAccountID := os.Getenv(AwsAccountIDEnv)
if awsAccountID == "" {
awsAccountID = AwsAccountIDDefault
}
farm := os.Getenv(AppFarmEnv)
if farm == "" {
farm = os.Getenv(AppFarmLegacyEnv)
if farm == "" {
farm = AppFarmDefault
}
}
logLevel := os.Getenv(LogLevelEnv)
if logLevel == "" {
logLevel = LogLevelDefault
}
quiet := getEnvBool(LogQuietModeEnv, false)
consoleWriter := getEnvBool(LogConsoleWriterEnv, false)
consoleColour := getEnvBool(LogConsoleWriterEnv, false)
return &Config{
LogLevel: logLevel,
AppName: appName,
AppVersion: appVersion,
AwsRegion: awsRegion,
AwsAccountID: awsAccountID,
Product: product,
Farm: farm,
Quiet: quiet,
ConsoleWriter: consoleWriter,
ConsoleColour: consoleColour,
TimeNow: time.Now,
}
}
func (c *Config) isLocal() bool {
return c.Farm == AppFarmDefault && c.AwsAccountID == AwsAccountIDDefault
}
func (c *Config) Level() zerolog.Level {
return c.ToLevel(c.LogLevel)
}
func (c *Config) ToLevel(logLevel string) zerolog.Level {
var lvl zerolog.Level
switch logLevel {
case "DEBUG", "Debug", "debug":
lvl = zerolog.DebugLevel
case "WARN", "Warn", "warn":
lvl = zerolog.WarnLevel
case "ERROR", "Error", "error":
lvl = zerolog.ErrorLevel
case "FATAL", "Fatal", "fatal":
lvl = zerolog.FatalLevel
case "PANIC", "Panic", "panic":
lvl = zerolog.PanicLevel
default:
lvl = zerolog.InfoLevel
}
return lvl
}
func (c *Config) getWriter() io.Writer {
// Default to Stdout, but if running in QuietMode then set the logger to silently NoOp
var writer io.Writer = os.Stdout
if c.Quiet {
writer = io.Discard
}
// NOTE: only allow ConsoleWriter to be configured if we are NOT production
// as the ConsoleWriter is NOT performant and should just be used for local only
if c.isLocal() && c.ConsoleWriter {
writer = zerolog.ConsoleWriter{
Out: writer,
TimeFormat: time.RFC3339,
NoColor: !c.ConsoleColour,
FormatMessage: c.formatMessage,
FormatTimestamp: c.formatTimestamp,
}
}
return writer
}
func (c *Config) formatMessage(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("event=\"%s\"", i)
}
func (c *Config) formatTimestamp(i interface{}) string {
if i == nil {
return "nil"
}
timeString, ok := i.(string)
if !ok {
return "nil"
}
return timeString
}
// isValid returns nil if all the mandatory env_vars are correctly set,
// otherwise an error indicating the issue.
func (c *Config) isValid() error {
if c.AppName == "" {
return errors.Errorf("config.AppName is empty - missing APP or APP_NAME environment variable?")
}
if c.AwsRegion == "" {
return errors.Errorf("config.AwsRegion is empty - missing AWS_REGION environment variable?")
}
if c.Product == "" {
return errors.Errorf("config.Product is empty - missing PRODUCT environment variable?")
}
return nil
}
// GetBool gets the environment variable for 'key' if present, otherwise returns 'fallback'.
func getEnvBool(key string, defaultValue bool) bool {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}
b, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return b
}