-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
logrus.go
140 lines (118 loc) · 2.75 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
package logrusx
import (
"github.com/sirupsen/logrus"
"github.com/ory/viper"
"github.com/ory/x/stringsx"
)
func newLogger(o *options) *logrus.Logger {
l := o.l
if l == nil {
l = logrus.New()
}
if o.exitFunc != nil {
l.ExitFunc = o.exitFunc
}
if o.level != nil {
l.Level = *o.level
} else {
var err error
l.Level, err = logrus.ParseLevel(stringsx.Coalesce(
viper.GetString("log.level"),
viper.GetString("LOG_LEVEL")))
if err != nil {
l.Level = logrus.InfoLevel
}
}
if o.formatter != nil {
l.Formatter = o.formatter
} else {
switch stringsx.Coalesce(o.format, viper.GetString("log.format"), viper.GetString("LOG_FORMAT")) {
case "json":
l.Formatter = &logrus.JSONFormatter{
PrettyPrint: l.IsLevelEnabled(logrus.DebugLevel),
}
default:
l.Formatter = &logrus.TextFormatter{
DisableQuote: true,
DisableTimestamp: false,
FullTimestamp: true,
}
}
}
for _, hook := range o.hooks {
l.AddHook(hook)
}
l.ReportCaller = o.reportCaller || l.IsLevelEnabled(logrus.TraceLevel)
return l
}
type options struct {
l *logrus.Logger
level *logrus.Level
formatter logrus.Formatter
format string
reportCaller bool
exitFunc func(int)
leakSensitive bool
hooks []logrus.Hook
}
type Option func(*options)
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 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 newOptions(opts []Option) *options {
o := new(options)
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{
leakSensitive: o.leakSensitive ||
viper.GetBool("log.leak_sensitive_values") || viper.GetBool("LOG_LEAK_SENSITIVE_VALUES"),
Entry: newLogger(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")
}