-
Notifications
You must be signed in to change notification settings - Fork 8
/
options.go
160 lines (131 loc) · 3.55 KB
/
options.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
package vlog
import (
"context"
"fmt"
"io"
"strings"
"github.com/sethvargo/go-envconfig"
)
const defaultEnvPrefix = "VLOG_"
// LogLevelTrace and others represent log levels
const (
LogLevelTrace = "trace" // 5
LogLevelDebug = "debug" // 4
LogLevelInfo = "info" // 3
LogLevelWarn = "warn" // 2
LogLevelError = "error" // 1
LogLevelNull = "null" // 0
)
var levelStringMap = map[string]int{
LogLevelTrace: 5,
LogLevelDebug: 4,
LogLevelInfo: 3,
LogLevelWarn: 2,
LogLevelError: 1,
LogLevelNull: 0,
}
// Options represents the options for a VLogger
type Options struct {
Level int
LevelString string `env:"LOG_LEVEL"`
Filepath string `env:"LOG_FILE"`
LogPrefix string `env:"LOG_PREFIX"`
OutputWriter io.Writer
EnvPrefix string
AppMeta interface{}
PreLogHook LogHookFunc
}
type LogHookFunc func([]byte)
// OptionsModifier is a options modifier function
type OptionsModifier func(*Options)
func newOptions(mods ...OptionsModifier) *Options {
opts := defaultOptions()
for _, mod := range mods {
mod(opts)
}
envPrefix := defaultEnvPrefix
if opts.EnvPrefix != "" {
envPrefix = opts.EnvPrefix
}
opts.finalize(envPrefix)
return opts
}
// WithWriter configures the logger to write its logs to the supplied writer
func WithWriter(writer io.Writer) OptionsModifier {
return func(opt *Options) {
opt.OutputWriter = writer
}
}
// Level sets the logging level to one of error, warn, info, debug, or trace
func Level(level string) OptionsModifier {
return func(opt *Options) {
opt.Level = logLevelValFromString(level)
}
}
// ToFile sets the logger to open the file specified and write logs to it
func ToFile(filepath string) OptionsModifier {
return func(opt *Options) {
opt.Filepath = filepath
}
}
// LogPrefix sets a prefix on all of the log messages
func LogPrefix(logPrefix string) OptionsModifier {
return func(opt *Options) {
opt.LogPrefix = logPrefix
}
}
// EnvPrefix sets a prefix for evaluating logger settings from env
func EnvPrefix(envPrefix string) OptionsModifier {
return func(opt *Options) {
opt.EnvPrefix = envPrefix
}
}
// AppMeta sets the AppMeta object to be included with structured logs
func AppMeta(meta interface{}) OptionsModifier {
return func(opt *Options) {
opt.AppMeta = meta
}
}
// PreLogHook sets a function to be run before each logged value
func PreLogHook(hook LogHookFunc) OptionsModifier {
return func(opt *Options) {
opt.PreLogHook = hook
}
}
func defaultOptions() *Options {
o := &Options{
Level: logLevelValFromString(LogLevelInfo),
LevelString: "",
Filepath: "",
LogPrefix: "",
EnvPrefix: "",
AppMeta: nil,
}
return o
}
// finalize "locks in" the options by overriding any existing options with the version from the environment, and setting the default logger if needed
func (o *Options) finalize(envPrefix string) {
envOpts := Options{}
if err := envconfig.ProcessWith(context.Background(), &envOpts, envconfig.PrefixLookuper(envPrefix, envconfig.OsLookuper())); err != nil {
fmt.Printf("[vlog] failed to ProcessWith environment config:" + err.Error())
return
}
o.replaceFieldsIfNeeded(&envOpts)
}
func (o *Options) replaceFieldsIfNeeded(replacement *Options) {
if replacement.LevelString != "" {
o.Level = logLevelValFromString(replacement.LevelString)
}
if replacement.Filepath != "" {
o.Filepath = replacement.Filepath
}
if replacement.LogPrefix != "" {
o.LogPrefix = replacement.LogPrefix
}
}
func logLevelValFromString(level string) int {
if level, ok := levelStringMap[strings.ToLower(level)]; ok {
return level
}
return 3
}