-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
151 lines (134 loc) · 3.19 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
package logging
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/sirupsen/logrus"
)
type Config struct {
Level string `json:"level"`
Formatter formatter `json:"formatter"`
LocalLogger bool `json:"localLogger"`
AddSource bool `json:"addSource"`
}
type formatter struct {
Format string `json:"format"`
Data map[string]interface{} `json:"data"`
}
type loggingConfig Config
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
log = (*logger)(logrus.New())
err := unmarshal((*loggingConfig)(c))
if err != nil {
return err
}
return c.SetLogger()
}
func (c *Config) UnmarshalJSON(data []byte) error {
log = (*logger)(logrus.New())
err := json.Unmarshal(data, (*loggingConfig)(c))
if err != nil {
return err
}
return c.SetLogger()
}
func (c *Config) SetLogger() (err error) {
err = c.parseFormatter()
if err != nil {
return err
}
err = c.parseLevel()
if err != nil {
return err
}
err = c.unmarshalFormatter()
if err != nil {
return err
}
c.setGlobal()
return nil
}
func (c *Config) setGlobal() {
if c.LocalLogger {
return
}
logrus.SetFormatter(log.Formatter)
logrus.SetLevel(log.Level)
logrus.SetReportCaller(log.ReportCaller)
log = (*logger)(logrus.StandardLogger())
}
func (c *Config) unmarshalFormatter() error {
formatterData, err := json.Marshal(c.Formatter.Data)
if err != nil {
return err
}
return json.Unmarshal(formatterData, log.Formatter)
}
func (c *Config) parseLevel() error {
if c.Level == "" {
log.Level = logrus.InfoLevel
return nil
}
level, err := logrus.ParseLevel(c.Level)
if err != nil {
return err
}
log.Level = level
return nil
}
const (
FormatterText = "text"
FormatterJSON = "json"
)
func (c *Config) parseFormatter() error {
switch c.Formatter.Format {
case FormatterJSON:
log.Formatter = &logrus.JSONFormatter{}
case FormatterText, "":
log.Formatter = &logrus.TextFormatter{}
default:
return fmt.Errorf("%s formatter not supported", c.Formatter)
}
return nil
}
// Slog constructs a slog.Logger with the Formatter and Level from config.
func (c *Config) Slog() *slog.Logger {
logger := slog.Default()
var level slog.Level
if err := level.UnmarshalText([]byte(c.Level)); err != nil {
logger.Warn("invalid config, using default slog", "err", err)
return logger
}
opts := &slog.HandlerOptions{
AddSource: c.AddSource,
Level: level,
ReplaceAttr: c.fieldMapToPlaceKey(),
}
switch c.Formatter.Format {
case FormatterText:
return slog.New(slog.NewTextHandler(os.Stderr, opts))
case FormatterJSON:
return slog.New(slog.NewJSONHandler(os.Stderr, opts))
case "":
logger.Warn("no slog format in config, using text handler")
default:
logger.Warn("unknown slog format in config, using text handler", "format", c.Formatter.Format)
}
return slog.New(slog.NewTextHandler(os.Stderr, opts))
}
func (c *Config) fieldMapToPlaceKey() func(groups []string, a slog.Attr) slog.Attr {
fieldMap, ok := c.Formatter.Data["fieldmap"].(map[string]interface{})
if !ok {
return nil
}
return func(groups []string, a slog.Attr) slog.Attr {
for key, newKey := range fieldMap {
if a.Key == key {
a.Key = newKey.(string)
return a
}
}
return a
}
}