-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
129 lines (105 loc) · 3.32 KB
/
logger.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
package logger
import (
"context"
"io"
"os"
"time"
"github.com/sirupsen/logrus"
)
// Fields type, used to pass to `WithFields`.
type Fields map[string]interface{}
// NowFunc is a typedef for a function which returns the current time
type NowFunc func() time.Time
// Logger is our logger with the needed structured logger we use
type Logger struct {
logrusLogger *logrus.Logger
now NowFunc
output io.Writer
level Level
reportCaller bool
}
func (logger *Logger) applyOptions(opts ...LoggerOption) {
for _, opt := range opts {
opt.Apply(logger)
}
logger.logrusLogger.SetOutput(logger.output)
logger.logrusLogger.SetLevel(mapLevelToLogrusLevel(logger.level))
}
// New creates and returns a new logger with supplied options
func New(opts ...LoggerOption) *Logger {
logrusLogger := logrus.New()
logrusLogger.SetFormatter(&logrus.JSONFormatter{})
logger := &Logger{
logrusLogger: logrusLogger,
now: NowFunc(time.Now),
output: os.Stdout,
level: LevelWarn,
reportCaller: true,
}
logger.applyOptions(opts...)
return logger
}
func (logger *Logger) entry() *Entry {
return &Entry{logger: logger, fields: Fields{}}
}
const errorKey = "error"
// WithError is a convenience wrapper for WithField("error", err)
func (logger *Logger) WithError(err error) *Entry {
return logger.WithField(errorKey, err)
}
// WithField forwards a logging call with a field
func (logger *Logger) WithField(key string, value interface{}) *Entry {
return logger.entry().WithField(key, value)
}
// WithFields forwards a logging call with fields
func (logger *Logger) WithFields(fields Fields) *Entry {
return logger.entry().WithFields(fields)
}
// WithContext forwards a logging call with fields
func (logger *Logger) WithContext(ctx context.Context) *Entry {
return logger.entry().WithContext(ctx)
}
// OutputHandler returns logger output handler
func (logger *Logger) OutputHandler() io.Writer {
return logger.output
}
// Info forwards a logging call in the (format, args) format
func (logger *Logger) Info(args ...interface{}) {
logger.entry().Info(args...)
}
// Infof forwards a logging call in the (format, args) format
func (logger *Logger) Infof(format string, args ...interface{}) {
logger.entry().Infof(format, args...)
}
// Error forwards an error logging call
func (logger *Logger) Error(args ...interface{}) {
logger.entry().Error(args...)
}
// Errorf forwards an error logging call
func (logger *Logger) Errorf(format string, args ...interface{}) {
logger.entry().Errorf(format, args...)
}
// Debug forwards a debugging logging call
func (logger *Logger) Debug(args ...interface{}) {
logger.entry().Debug(args...)
}
// Debugf forwards a debugging logging call
func (logger *Logger) Debugf(format string, args ...interface{}) {
logger.entry().Debugf(format, args...)
}
// Warn forwards a warning logging call
func (logger *Logger) Warn(args ...interface{}) {
logger.entry().Warn(args...)
}
// Warnf forwards a warning logging call
func (logger *Logger) Warnf(format string, args ...interface{}) {
logger.entry().Warnf(format, args...)
}
// Fatal forwards a fatal logging call
func (logger *Logger) Fatal(args ...interface{}) {
logger.entry().Fatal(args...)
}
// Fatalf forwards a fatal logging call
func (logger *Logger) Fatalf(format string, args ...interface{}) {
logger.entry().Fatalf(format, args...)
}