-
Notifications
You must be signed in to change notification settings - Fork 2
/
pfxlog.go
executable file
·147 lines (124 loc) · 3.64 KB
/
pfxlog.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
package pfxlog
import (
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"os"
"sort"
)
func init() {
// cover cases where ContextLogger is used in a package init function.
globalOptions = &Options{StandardLogger: logrus.StandardLogger()}
}
func GlobalInit(level logrus.Level, options *Options) {
if defaultEnv("PFXLOG_NO_JSON", false) || terminal.IsTerminal(int(os.Stdout.Fd())) {
logrus.SetFormatter(NewFormatter(options))
} else {
logrus.SetFormatter(&logrus.JSONFormatter{TimestampFormat: options.JsonTimestampFormat})
}
logrus.SetLevel(level)
logrus.SetReportCaller(true)
globalOptions = options
for _, logLevel := range logrus.AllLevels {
logger := CloneLogger(options.StandardLogger)
logger.Level = logLevel
if _, found := globalOptions.Loggers[logLevel]; !found {
globalOptions.Loggers[logLevel] = logger
}
}
}
func GlobalConfig(f func(*Options) *Options) {
globalOptions = f(globalOptions)
}
func Logger() *Builder {
return &Builder{Entry: logrus.NewEntry(globalOptions.StandardLogger)}
}
func ContextLogger(context string) *Builder {
return &Builder{Entry: globalOptions.StandardLogger.WithField("_context", context)}
}
type Builder struct {
channels []string
*logrus.Entry
}
func (self *Builder) Clone() *Builder {
clone := &Builder{
Entry: self.Entry.Dup(),
}
if len(self.channels) > 0 {
clone.channels = make([]string, len(self.channels))
copy(clone.channels, self.channels)
}
return clone
}
type Wirer interface {
WireEntry(entry *logrus.Entry) *logrus.Entry
}
type EntryWireF func(entry *logrus.Entry) *logrus.Entry
func (self EntryWireF) WireEntry(entry *logrus.Entry) *logrus.Entry {
return self(entry)
}
func (self *Builder) Wire(wirer Wirer) *Builder {
self.Entry = wirer.WireEntry(self.Entry)
return self
}
func (self *Builder) Data(data interface{}) *Builder {
if globalOptions.DataFielder != nil {
self.Entry = globalOptions.DataFielder(data, self.Entry)
}
return self
}
func (self *Builder) Enabled(data interface{}) *Builder {
if globalOptions.EnabledChecker != nil && !globalOptions.EnabledChecker(data) {
self.Entry.Logger = globalOptions.Loggers[logrus.PanicLevel]
}
return self
}
func (self *Builder) Channels(channels ...string) *Builder {
for _, channel := range channels {
if _, found := globalOptions.ActiveChannels[channel]; found {
self.Entry = self.Entry.WithField("_channels", channels)
return self
}
}
self.Entry.Logger = globalOptions.Loggers[logrus.PanicLevel]
return self
}
func (self *Builder) WithChannels(channels ...string) *Builder {
chidx := make(map[string]struct{})
for _, ch := range self.channels {
chidx[ch] = struct{}{}
}
for _, channel := range channels {
if level, found := globalOptions.ChannelLogLevelOverrides[channel]; found {
if level > self.Entry.Logger.Level {
self.Entry.Logger = globalOptions.Loggers[level]
}
}
chidx[channel] = struct{}{}
}
self.channels = []string{}
for k, _ := range chidx {
self.channels = append(self.channels, k)
}
sort.Slice(self.channels, func(i, j int) bool {
return self.channels[i] < self.channels[j]
})
self.Entry = self.Entry.WithField("_channels", self.channels)
return self
}
func (self *Builder) SetContext(context string) *Builder {
self.Entry = self.Entry.WithField("_context", context)
return self
}
func ChannelLogger(channels ...string) *Builder {
return Logger().WithChannels(channels...)
}
func LevelLogger(level logrus.Level) *logrus.Logger {
return globalOptions.Loggers[level]
}
func SetFormatter(f logrus.Formatter) {
globalOptions.StandardLogger.SetFormatter(f)
for _, logger := range globalOptions.Loggers {
logger.SetFormatter(f)
}
}
var globalOptions *Options