-
Notifications
You must be signed in to change notification settings - Fork 53
/
config.go
123 lines (101 loc) · 2.84 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
package log
import (
"log/slog"
"strings"
"github.com/omni-network/omni/lib/errors"
"github.com/muesli/termenv"
"github.com/spf13/pflag"
)
const (
FormatCLI = "cli"
FormatConsole = "console"
FormatJSON = "json"
FormatLogfmt = "logfmt"
ColorForce = "force"
ColorDisable = "disable"
ColorAuto = "auto"
)
//nolint:gochecknoglobals // Static mapping.
var (
levelDebug = strings.ToLower(slog.LevelDebug.String())
levelInfo = strings.ToLower(slog.LevelInfo.String())
levelWarn = strings.ToLower(slog.LevelWarn.String())
levelError = strings.ToLower(slog.LevelError.String())
levels = []string{levelDebug, levelInfo, levelWarn, levelError}
)
//nolint:gochecknoglobals // Static mapping.
var loggerFuncs = map[string]func(...func(*options)) *slog.Logger{
FormatConsole: newConsoleLogger,
FormatJSON: newJSONLogger,
FormatLogfmt: newLogfmtLogger,
FormatCLI: newCLILogger,
}
//nolint:gochecknoglobals // Static mapping.
var colors = map[string]termenv.Profile{
ColorForce: termenv.TrueColor,
ColorDisable: termenv.Ascii,
ColorAuto: termenv.ColorProfile(),
}
// DefaultConfig returns a default config.
func DefaultConfig() Config {
return Config{
Level: levelInfo,
Color: ColorAuto,
Format: FormatConsole,
}
}
type Config struct {
Level string
Color string
Format string
}
func (c Config) color() (termenv.Profile, error) {
color := c.Color
if c.Color == "" {
color = ColorAuto
}
resp, ok := colors[color]
if !ok {
return 0, errors.New("invalid color", "color", c.Color)
}
return resp, nil
}
func (c Config) level() (slog.Level, error) {
var level slog.Level
if err := level.UnmarshalText([]byte(c.Level)); err != nil {
return slog.Level(0), errors.Wrap(err, "parse log level")
}
return level, nil
}
func (c Config) loggerFunc() (func(...func(*options)) *slog.Logger, error) {
f, ok := loggerFuncs[c.Format]
if !ok {
return nil, errors.New("invalid format", "format", c.Format)
}
return f, nil
}
// make returns a new global as per the config.
func (c Config) make() (*slog.Logger, error) {
level, err := c.level()
if err != nil {
return nil, errors.Wrap(err, "parse log level")
}
color, err := c.color()
if err != nil {
return nil, errors.Wrap(err, "parse log color")
}
loggerFunc, err := c.loggerFunc()
if err != nil {
return nil, errors.Wrap(err, "parse log format")
}
return loggerFunc(func(o *options) {
o.Level = level
o.Color = color
}), nil
}
// BindFlags binds the standard flags to provide logging config at runtime.
func BindFlags(flags *pflag.FlagSet, cfg *Config) {
flags.StringVar(&cfg.Level, "log-level", cfg.Level, "Log level; debug, info, warn, error")
flags.StringVar(&cfg.Color, "log-color", cfg.Color, "Log color (only applicable to console format); auto, force, disable")
flags.StringVar(&cfg.Format, "log-format", cfg.Format, "Log format; console, json")
}