This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
log.go
54 lines (43 loc) · 1.42 KB
/
log.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
package logger
import (
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
)
var G = GetLogger
type loggerKey struct{}
// WithLogger returns a new context with the provided logger. Use in
// combination with logger.WithField(s) for great effect.
func WithLogger(ctx context.Context, logger logrus.FieldLogger) context.Context {
return context.WithValue(ctx, loggerKey{}, logger)
}
// GetLogger retrieves the current logger from the context. If no logger is
// available, the default logger is returned.
func GetLogger(ctx context.Context) logrus.FieldLogger {
logger := ctx.Value(loggerKey{})
if logger == nil {
l := logrus.StandardLogger()
l.SetFormatter(&logrus.TextFormatter{
DisableQuote: true,
})
return l
}
return logger.(logrus.FieldLogger)
}
func WithField(ctx context.Context, key string, value interface{}) context.Context {
return WithLogger(ctx, GetLogger(ctx).WithField(key, value))
}
func WithFields(ctx context.Context, fields map[string]interface{}) context.Context {
return WithLogger(ctx, GetLogger(ctx).WithFields(fields))
}
// ShouldJSON returns JSON'd version of object, and if it cannot, it will log the error, and returns the "%+v" formatted
// version
func ShouldJSON(ctx context.Context, o interface{}) string {
data, err := json.Marshal(o)
if err != nil {
GetLogger(ctx).WithError(err).Error("Could not serialize value")
return fmt.Sprintf("%+v", o)
}
return string(data)
}