-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (134 loc) · 3.91 KB
/
main.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
package log
import (
"context"
"io"
"os"
"github.com/segmentio/go-loggly"
"github.com/sirupsen/logrus"
)
// DefaultLogger represents the default logger that is not bound to any specific
// context.
var DefaultLogger *Entry
const (
PanicLevel = logrus.PanicLevel
ErrorLevel = logrus.ErrorLevel
WarnLevel = logrus.WarnLevel
InfoLevel = logrus.InfoLevel
DebugLevel = logrus.DebugLevel
)
// Entry repre
type Entry struct {
entry logrus.Entry
isTesting bool
}
// F wraps the logrus.Fields type for the convenience of typing less.
type F logrus.Fields
// LogglyHook sends logs to loggly
type LogglyHook struct {
client *loggly.Client
host string
FilteredKeys map[string]bool
}
// New creates a new logger, starting at a WARN level and including the current
// processes pid as a field.
func New() *Entry {
l := logrus.New()
l.Level = logrus.WarnLevel
l.Formatter.(*logrus.TextFormatter).FullTimestamp = true
l.Formatter.(*logrus.TextFormatter).TimestampFormat = timeStampFormat
return &Entry{entry: *logrus.NewEntry(l).WithField("pid", os.Getpid())}
}
// Set establishes a new context to which the provided sub-logger is bound
func Set(parent context.Context, logger *Entry) context.Context {
return context.WithValue(parent, &loggerContextKey, logger)
}
// Ctx returns the logger bound to the provided context, otherwise
// providing the default logger.
func Ctx(ctx context.Context) *Entry {
if ctx == nil {
return DefaultLogger
}
found := ctx.Value(&loggerContextKey)
if found == nil {
return DefaultLogger
}
return found.(*Entry)
}
// PushContext is a helper method to derive a new context with a modified logger
// bound to it, where the logger is derived from the current value on the
// context.
func PushContext(parent context.Context, modFn func(*Entry) *Entry) context.Context {
current := Ctx(parent)
next := modFn(current)
return Set(parent, next)
}
func SetLevel(level logrus.Level) {
DefaultLogger.SetLevel(level)
}
func SetOut(out io.Writer) {
DefaultLogger.entry.Logger.Out = out
}
func WithField(key string, value interface{}) *Entry {
result := DefaultLogger.WithField(key, value)
return result
}
func WithFields(fields F) *Entry {
return DefaultLogger.WithFields(fields)
}
func WithStack(stackProvider interface{}) *Entry {
return DefaultLogger.WithStack(stackProvider)
}
// ===== Delegations =====
// Debugf logs a message at the debug severity.
func Debugf(format string, args ...interface{}) {
DefaultLogger.Debugf(format, args...)
}
// Debug logs a message at the debug severity.
func Debug(args ...interface{}) {
DefaultLogger.Debug(args...)
}
// Infof logs a message at the Info severity.
func Infof(format string, args ...interface{}) {
DefaultLogger.Infof(format, args...)
}
// Info logs a message at the Info severity.
func Info(args ...interface{}) {
DefaultLogger.Info(args...)
}
// Warnf logs a message at the Warn severity.
func Warnf(format string, args ...interface{}) {
DefaultLogger.Warnf(format, args...)
}
// Warn logs a message at the Warn severity.
func Warn(args ...interface{}) {
DefaultLogger.Warn(args...)
}
// Errorf logs a message at the Error severity.
func Errorf(format string, args ...interface{}) {
DefaultLogger.Errorf(format, args...)
}
// Error logs a message at the Error severity.
func Error(args ...interface{}) {
DefaultLogger.Error(args...)
}
// Fatalf logs a message at the Fatal severity.
func Fatalf(format string, args ...interface{}) {
DefaultLogger.Fatalf(format, args...)
}
// Fatal logs a message at the Fatal severity.
func Fatal(args ...interface{}) {
DefaultLogger.Fatal(args...)
}
// Panicf logs a message at the Panic severity.
func Panicf(format string, args ...interface{}) {
DefaultLogger.Panicf(format, args...)
}
// Panic logs a message at the Panic severity.
func Panic(args ...interface{}) {
DefaultLogger.Panic(args...)
}
type contextKey string
var loggerContextKey = contextKey("logger")
func init() {
DefaultLogger = New()
}