-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixed_logger.go
149 lines (113 loc) · 4.52 KB
/
prefixed_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package log
import (
"fmt"
"io"
"github.com/neighborly/go-errors"
"github.com/sirupsen/logrus"
)
type PrefixedLogger struct {
Prefix string
LoggerInstance *logrus.Logger
}
func NewPrefixedLogger(prefix string, instance Logger) PrefixedLogger {
var _logger *logrus.Logger
if instance == nil {
_logger = New(true, "info")
} else if logrusLogger, ok := instance.(*logrus.Logger); ok {
_logger = logrusLogger
}
return PrefixedLogger{
Prefix: prefix,
LoggerInstance: _logger,
}
}
func (l *PrefixedLogger) prefixArgs(args []interface{}) []interface{} {
return append([]interface{}{l.Prefix + ": "}, args...)
}
func (l *PrefixedLogger) prefixMsg(message string) string {
return fmt.Sprintf("%s: %s", l.Prefix, message)
}
// implement logger interface
func (l *PrefixedLogger) Info(args ...interface{}) {
l.LoggerInstance.Info(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Infof(message string, args ...interface{}) {
l.LoggerInstance.Infof(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Debug(args ...interface{}) {
l.LoggerInstance.Debug(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Debugf(message string, args ...interface{}) {
l.LoggerInstance.Debugf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Error(args ...interface{}) {
l.LoggerInstance.Error(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Errorf(message string, args ...interface{}) {
l.LoggerInstance.Errorf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Warn(args ...interface{}) {
l.LoggerInstance.Warn(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Warnf(message string, args ...interface{}) {
l.LoggerInstance.Warnf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Fatal(args ...interface{}) {
l.LoggerInstance.Fatal(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Fatalf(message string, args ...interface{}) {
l.LoggerInstance.Fatalf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Panic(args ...interface{}) {
l.LoggerInstance.Panic(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) Panicf(message string, args ...interface{}) {
l.LoggerInstance.Panicf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) Writer() *io.PipeWriter {
return l.LoggerInstance.Writer()
}
// fields
func (l *PrefixedLogger) InfoWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Info(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) InfoWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Infof(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) DebugWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Debug(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) DebugWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Debugf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) ErrorWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Error(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) ErrorWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Errorf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) WarnWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Warn(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) WarnWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Warnf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) FatalWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Fatal(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) FatalWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Fatalf(l.prefixMsg(message), args...)
}
func (l *PrefixedLogger) PanicWithFields(fields Fields, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Panic(l.prefixArgs(args)...)
}
func (l *PrefixedLogger) PanicWithFieldsf(fields Fields, message string, args ...interface{}) {
l.LoggerInstance.WithFields(logrus.Fields(fields)).Panicf(l.prefixMsg(message), args...)
}
// error wrapping
func (l *PrefixedLogger) PrefixError(err error, msg string) error {
return errors.Wrapf(err, "%s: %s", l.Prefix, msg)
}
func (l *PrefixedLogger) WrapError(err error) error {
return errors.Wrapf(err, "%s: ", l.Prefix)
}