-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (143 loc) · 4.27 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
170
171
172
173
174
175
176
177
178
179
180
181
package log
import (
"net/http"
"os"
"time"
"github.com/segmentio/go-loggly"
"github.com/sirupsen/logrus"
"github.com/payshares/go/support/http/mutil"
"golang.org/x/net/context"
)
// 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 {
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() (result *Entry) {
l := logrus.New()
l.Level = logrus.WarnLevel
result = &Entry{
Entry: *logrus.NewEntry(l).WithField("pid", os.Getpid()),
}
return
}
// 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, &contextKey, logger)
}
// Ctx returns the logger bound to the provided context, otherwise
// providing the default logger.
func Ctx(ctx context.Context) *Entry {
found := ctx.Value(&contextKey)
if found == nil {
return DefaultLogger
}
return found.(*Entry)
}
// HTTPMiddleware is a middleware function that wraps the provided handler in a
// middleware that logs requests to the default logger.
func HTTPMiddleware(in http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mw := mutil.WrapWriter(w)
// TODO: migrate to go 1.7 context
ctx := context.TODO()
// TODO: add request id support
// logger := log.WithField("req", middleware.GetReqID(*c))
logStartOfRequest(ctx, r)
then := time.Now()
in.ServeHTTP(mw, r)
duration := time.Now().Sub(then)
logEndOfRequest(ctx, duration, mw)
})
}
// 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 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...)
}
// 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...)
}
// StartTest shifts the default logger into "test" mode. See Entry's
// documentation for the StartTest() method for more info.
func StartTest(level logrus.Level) func() []*logrus.Entry {
return DefaultLogger.StartTest(level)
}
var contextKey = 0
func init() {
DefaultLogger = New()
}