-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
logrus.go
230 lines (196 loc) · 5.28 KB
/
logrus.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package logrusx
import (
"bytes"
_ "embed"
"io"
"net/http"
"os"
"github.com/sirupsen/logrus"
gelf "github.com/seatgeek/logrus-gelf-formatter"
"github.com/ory/x/stringsx"
)
type (
options struct {
l *logrus.Logger
level *logrus.Level
formatter logrus.Formatter
format string
reportCaller bool
exitFunc func(int)
leakSensitive bool
redactionText string
hooks []logrus.Hook
c configurator
}
Option func(*options)
nullConfigurator struct{}
configurator interface {
Bool(key string) bool
String(key string) string
}
)
//go:embed config.schema.json
var ConfigSchema string
const ConfigSchemaID = "ory://logging-config"
// AddConfigSchema adds the logging schema to the compiler.
// The interface is specified instead of `jsonschema.Compiler` to allow the use of any jsonschema library fork or version.
func AddConfigSchema(c interface {
AddResource(url string, r io.Reader) error
}) error {
return c.AddResource(ConfigSchemaID, bytes.NewBufferString(ConfigSchema))
}
func newLogger(parent *logrus.Logger, o *options) *logrus.Logger {
l := parent
if l == nil {
l = logrus.New()
}
if o.exitFunc != nil {
l.ExitFunc = o.exitFunc
}
for _, hook := range o.hooks {
l.AddHook(hook)
}
setLevel(l, o)
setFormatter(l, o)
l.ReportCaller = o.reportCaller || l.IsLevelEnabled(logrus.TraceLevel)
return l
}
func setLevel(l *logrus.Logger, o *options) {
if o.level != nil {
l.Level = *o.level
} else {
var err error
l.Level, err = logrus.ParseLevel(stringsx.Coalesce(
o.c.String("log.level"),
os.Getenv("LOG_LEVEL")))
if err != nil {
l.Level = logrus.InfoLevel
}
}
}
func setFormatter(l *logrus.Logger, o *options) {
if o.formatter != nil {
l.Formatter = o.formatter
} else {
var unknownFormat bool // we first have to set the formatter before we can complain about the unknown format
format := stringsx.SwitchExact(stringsx.Coalesce(o.format, o.c.String("log.format"), os.Getenv("LOG_FORMAT")))
switch {
case format.AddCase("json"):
l.Formatter = &logrus.JSONFormatter{PrettyPrint: false}
case format.AddCase("json_pretty"):
l.Formatter = &logrus.JSONFormatter{PrettyPrint: true}
case format.AddCase("gelf"):
l.Formatter = new(gelf.GelfFormatter)
default:
unknownFormat = true
fallthrough
case format.AddCase("text"), format.AddCase(""):
l.Formatter = &logrus.TextFormatter{
DisableQuote: true,
DisableTimestamp: false,
FullTimestamp: true,
}
}
if unknownFormat {
l.WithError(format.ToUnknownCaseErr()).Warn("got unknown \"log.format\", falling back to \"text\"")
}
}
}
func ForceLevel(level logrus.Level) Option {
return func(o *options) {
o.level = &level
}
}
func ForceFormatter(formatter logrus.Formatter) Option {
return func(o *options) {
o.formatter = formatter
}
}
func WithConfigurator(c configurator) Option {
return func(o *options) {
o.c = c
}
}
func ForceFormat(format string) Option {
return func(o *options) {
o.format = format
}
}
func WithHook(hook logrus.Hook) Option {
return func(o *options) {
o.hooks = append(o.hooks, hook)
}
}
func WithExitFunc(exitFunc func(int)) Option {
return func(o *options) {
o.exitFunc = exitFunc
}
}
func ReportCaller(reportCaller bool) Option {
return func(o *options) {
o.reportCaller = reportCaller
}
}
func UseLogger(l *logrus.Logger) Option {
return func(o *options) {
o.l = l
}
}
func LeakSensitive() Option {
return func(o *options) {
o.leakSensitive = true
}
}
func RedactionText(text string) Option {
return func(o *options) {
o.redactionText = text
}
}
func (c *nullConfigurator) Bool(_ string) bool {
return false
}
func (c *nullConfigurator) String(_ string) string {
return ""
}
func newOptions(opts []Option) *options {
o := new(options)
o.c = new(nullConfigurator)
for _, f := range opts {
f(o)
}
return o
}
// New creates a new logger with all the important fields set.
func New(name string, version string, opts ...Option) *Logger {
o := newOptions(opts)
return &Logger{
opts: opts,
name: name,
version: version,
leakSensitive: o.leakSensitive || o.c.Bool("log.leak_sensitive_values"),
redactionText: stringsx.DefaultIfEmpty(o.redactionText, `Value is sensitive and has been redacted. To see the value set config key "log.leak_sensitive_values = true" or environment variable "LOG_LEAK_SENSITIVE_VALUES=true".`),
Entry: newLogger(o.l, o).WithFields(logrus.Fields{
"audience": "application", "service_name": name, "service_version": version}),
}
}
func NewAudit(name string, version string, opts ...Option) *Logger {
return New(name, version, opts...).WithField("audience", "audit")
}
func (l *Logger) UseConfig(c configurator) {
l.leakSensitive = l.leakSensitive || c.Bool("log.leak_sensitive_values")
l.redactionText = stringsx.DefaultIfEmpty(c.String("log.redaction_text"), l.redactionText)
o := newOptions(append(l.opts, WithConfigurator(c)))
setLevel(l.Entry.Logger, o)
setFormatter(l.Entry.Logger, o)
}
func (l *Logger) ReportError(r *http.Request, code int, err error, args ...interface{}) {
logger := l.WithError(err).WithRequest(r).WithField("http_response", map[string]interface{}{
"status_code": code,
})
switch {
case code < 500:
logger.Info(args...)
default:
logger.Error(args...)
}
}