-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
118 lines (100 loc) · 3.44 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
package log
import (
"fmt"
"io"
"os"
"time"
senv "github.com/caarlos0/env/v11"
"github.com/rs/zerolog"
)
const (
AppVerDefault = "1.0.0"
AwsAccountIDDefault = "development"
AppFarmDefault = "local"
LogLevelDefault = "INFO"
MissingDefault = "<missing>"
)
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 `env:"APP" envDefault:"<unknown>"` // The name of the application the log was generated from
AppVersion string `env:"APP_VERSION" envDefault:"1.0.0"` // The version of the application
AwsRegion string `env:"AWS_REGION" envDefault:"dev"` // the AWS region this code is running in
AwsAccountID string `env:"AWS_ACCOUNT_ID" envDefault:"development"` // the AWS account ID this code is running in
Product string `env:"PRODUCT" envDefault:"<unknown>"` // performance, engagmentment, etc.
Farm string `env:"FARM" envDefault:"local"` // The name of the farm or where the code is running
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"` // The logging level
Quiet bool `env:"QUIET_MODE" envDefault:"false"` // Are we running inside tests and we should be quiet?
ConsoleWriter bool `env:"CONSOLE_WRITER" envDefault:"false"` // If ConsoleWriter=true then key-value pair output
ConsoleColour bool `env:"CONSOLE_COLOUR" envDefault:"false"` // 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, error) {
c := Config{
TimeNow: time.Now,
}
err := senv.Parse(&c)
return &c, err
}
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
}