-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
165 lines (140 loc) · 4.09 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
161
162
163
164
165
package pfxlog
import (
"fmt"
"github.com/mgutz/ansi"
"github.com/sirupsen/logrus"
"os"
"strconv"
"strings"
"time"
)
type Options struct {
StartTimestamp time.Time
AbsoluteTime bool
TrimPrefix string
PanicLabel string
FatalLabel string
ErrorLabel string
WarningLabel string
InfoLabel string
DebugLabel string
TraceLabel string
TimestampColor string
FunctionColor string
FieldsColor string
DefaultFgColor string
PrettyTimestampFormat string
JsonTimestampFormat string
ActiveChannels map[string]struct{}
ChannelLogLevelOverrides map[string]logrus.Level
DataFielder func(data interface{}, entry *logrus.Entry) *logrus.Entry
EnabledChecker func(data interface{}) bool
StandardLogger *logrus.Logger
Loggers map[logrus.Level]*logrus.Logger
}
func DefaultOptions() *Options {
options := &Options{
StartTimestamp: time.Now(),
AbsoluteTime: false,
PrettyTimestampFormat: "2006-01-02 15:04:05.000",
JsonTimestampFormat: "2006-01-02T15:04:05.000Z",
StandardLogger: logrus.StandardLogger(),
Loggers: map[logrus.Level]*logrus.Logger{},
}
if defaultEnv("PFXLOG_USE_COLOR", false) {
return options.Color()
} else {
return options.NoColor()
}
}
func (options *Options) Starting(t time.Time) *Options {
options.StartTimestamp = t
return options
}
func (options *Options) StartingToday() *Options {
now := time.Now()
options.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
return options
}
func (options *Options) SetAbsoluteTime() *Options {
options.AbsoluteTime = true
return options
}
func (options *Options) SetTrimPrefix(prefix string) *Options {
options.TrimPrefix = prefix
return options
}
func (options *Options) SetActiveChannels(channels ...string) *Options {
for _, channel := range channels {
if options.ActiveChannels == nil {
options.ActiveChannels = make(map[string]struct{})
}
options.ActiveChannels[channel] = struct{}{}
}
return options
}
func (options *Options) SetChannelLogLevel(channel string, level logrus.Level) {
m := map[string]logrus.Level{}
for k, v := range options.ChannelLogLevelOverrides {
m[k] = v
}
m[channel] = level
options.ChannelLogLevelOverrides = m
}
func (options *Options) ClearChannelLogLevel(channel string) {
m := map[string]logrus.Level{}
for k, v := range options.ChannelLogLevelOverrides {
if k != channel {
m[k] = v
}
}
options.ChannelLogLevelOverrides = m
}
func (options *Options) Color() *Options {
options.PanicLabel = ansi.Red + " PANIC" + ansi.DefaultFG
options.FatalLabel = ansi.Red + " FATAL" + ansi.DefaultFG
options.ErrorLabel = ansi.Red + " ERROR" + ansi.DefaultFG
options.WarningLabel = ansi.Yellow + "WARNING" + ansi.DefaultFG
options.InfoLabel = ansi.White + " INFO" + ansi.DefaultFG
options.DebugLabel = ansi.Blue + " DEBUG" + ansi.DefaultFG
options.TraceLabel = ansi.LightBlack + " TRACE" + ansi.DefaultFG
options.TimestampColor = ansi.Blue
options.FunctionColor = ansi.Cyan
options.FieldsColor = ansi.LightCyan
options.DefaultFgColor = ansi.DefaultFG
return options
}
func (options *Options) NoColor() *Options {
options.PanicLabel = " PANIC"
options.FatalLabel = " FATAL"
options.ErrorLabel = " ERROR"
options.WarningLabel = "WARNING"
options.InfoLabel = " INFO"
options.DebugLabel = " DEBUG"
options.TraceLabel = " TRACE"
options.TimestampColor = ""
options.FunctionColor = ""
options.FieldsColor = ""
options.DefaultFgColor = ""
return options
}
func CloneLogger(logger *logrus.Logger) *logrus.Logger {
return &logrus.Logger{
Out: logger.Out,
Hooks: logger.Hooks,
Formatter: logger.Formatter,
ReportCaller: logger.ReportCaller,
Level: logger.Level,
ExitFunc: logger.ExitFunc,
}
}
func defaultEnv(env string, defaultValue bool) bool {
if envStr := strings.ToLower(os.Getenv(env)); envStr != "" {
if envValue, err := strconv.ParseBool(envStr); err == nil {
return envValue
} else {
_, _ = fmt.Fprintf(os.Stderr, "error parsing environment variable '%s' (%v)\n", env, err)
}
}
return defaultValue
}