summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/logr/logger.go
blob: c2386312f0868b647e8b47fd283401f918007418 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package logr

import (
	"fmt"
)

// Fields type, used to pass to `WithFields`.
type Fields map[string]interface{}

// Logger provides context for logging via fields.
type Logger struct {
	logr   *Logr
	fields Fields
}

// Logr returns the `Logr` instance that created this `Logger`.
func (logger Logger) Logr() *Logr {
	return logger.logr
}

// WithField creates a new `Logger` with any existing fields
// plus the new one.
func (logger Logger) WithField(key string, value interface{}) Logger {
	return logger.WithFields(Fields{key: value})
}

// WithFields creates a new `Logger` with any existing fields
// plus the new ones.
func (logger Logger) WithFields(fields Fields) Logger {
	l := Logger{logr: logger.logr}
	// if parent has no fields then avoid creating a new map.
	oldLen := len(logger.fields)
	if oldLen == 0 {
		l.fields = fields
		return l
	}

	l.fields = make(Fields, len(fields)+oldLen)
	for k, v := range logger.fields {
		l.fields[k] = v
	}
	for k, v := range fields {
		l.fields[k] = v
	}
	return l
}

// Log checks that the level matches one or more targets, and
// if so, generates a log record that is added to the Logr queue.
// Arguments are handled in the manner of fmt.Print.
func (logger Logger) Log(lvl Level, args ...interface{}) {
	status := logger.logr.IsLevelEnabled(lvl)
	if status.Enabled {
		rec := NewLogRec(lvl, logger, "", args, status.Stacktrace)
		logger.logr.enqueue(rec)
	}
}

// Trace is a convenience method equivalent to `Log(TraceLevel, args...)`.
func (logger Logger) Trace(args ...interface{}) {
	logger.Log(Trace, args...)
}

// Debug is a convenience method equivalent to `Log(DebugLevel, args...)`.
func (logger Logger) Debug(args ...interface{}) {
	logger.Log(Debug, args...)
}

// Print ensures compatibility with std lib logger.
func (logger Logger) Print(args ...interface{}) {
	logger.Info(args...)
}

// Info is a convenience method equivalent to `Log(InfoLevel, args...)`.
func (logger Logger) Info(args ...interface{}) {
	logger.Log(Info, args...)
}

// Warn is a convenience method equivalent to `Log(WarnLevel, args...)`.
func (logger Logger) Warn(args ...interface{}) {
	logger.Log(Warn, args...)
}

// Error is a convenience method equivalent to `Log(ErrorLevel, args...)`.
func (logger Logger) Error(args ...interface{}) {
	logger.Log(Error, args...)
}

// Fatal is a convenience method equivalent to `Log(FatalLevel, args...)`
// followed by a call to os.Exit(1).
func (logger Logger) Fatal(args ...interface{}) {
	logger.Log(Fatal, args...)
	logger.logr.exit(1)
}

// Panic is a convenience method equivalent to `Log(PanicLevel, args...)`
// followed by a call to panic().
func (logger Logger) Panic(args ...interface{}) {
	logger.Log(Panic, args...)
	panic(fmt.Sprint(args...))
}

//
// Printf style
//

// Logf checks that the level matches one or more targets, and
// if so, generates a log record that is added to the main
// queue (channel). Arguments are handled in the manner of fmt.Printf.
func (logger Logger) Logf(lvl Level, format string, args ...interface{}) {
	status := logger.logr.IsLevelEnabled(lvl)
	if status.Enabled {
		rec := NewLogRec(lvl, logger, format, args, status.Stacktrace)
		logger.logr.enqueue(rec)
	}
}

// Tracef is a convenience method equivalent to `Logf(TraceLevel, args...)`.
func (logger Logger) Tracef(format string, args ...interface{}) {
	logger.Logf(Trace, format, args...)
}

// Debugf is a convenience method equivalent to `Logf(DebugLevel, args...)`.
func (logger Logger) Debugf(format string, args ...interface{}) {
	logger.Logf(Debug, format, args...)
}

// Infof is a convenience method equivalent to `Logf(InfoLevel, args...)`.
func (logger Logger) Infof(format string, args ...interface{}) {
	logger.Logf(Info, format, args...)
}

// Printf ensures compatibility with std lib logger.
func (logger Logger) Printf(format string, args ...interface{}) {
	logger.Infof(format, args...)
}

// Warnf is a convenience method equivalent to `Logf(WarnLevel, args...)`.
func (logger Logger) Warnf(format string, args ...interface{}) {
	logger.Logf(Warn, format, args...)
}

// Errorf is a convenience method equivalent to `Logf(ErrorLevel, args...)`.
func (logger Logger) Errorf(format string, args ...interface{}) {
	logger.Logf(Error, format, args...)
}

// Fatalf is a convenience method equivalent to `Logf(FatalLevel, args...)`
// followed by a call to os.Exit(1).
func (logger Logger) Fatalf(format string, args ...interface{}) {
	logger.Logf(Fatal, format, args...)
	logger.logr.exit(1)
}

// Panicf is a convenience method equivalent to `Logf(PanicLevel, args...)`
// followed by a call to panic().
func (logger Logger) Panicf(format string, args ...interface{}) {
	logger.Logf(Panic, format, args...)
}

//
// Println style
//

// Logln checks that the level matches one or more targets, and
// if so, generates a log record that is added to the main
// queue (channel). Arguments are handled in the manner of fmt.Println.
func (logger Logger) Logln(lvl Level, args ...interface{}) {
	status := logger.logr.IsLevelEnabled(lvl)
	if status.Enabled {
		rec := NewLogRec(lvl, logger, "", args, status.Stacktrace)
		rec.newline = true
		logger.logr.enqueue(rec)
	}
}

// Traceln is a convenience method equivalent to `Logln(TraceLevel, args...)`.
func (logger Logger) Traceln(args ...interface{}) {
	logger.Logln(Trace, args...)
}

// Debugln is a convenience method equivalent to `Logln(DebugLevel, args...)`.
func (logger Logger) Debugln(args ...interface{}) {
	logger.Logln(Debug, args...)
}

// Infoln is a convenience method equivalent to `Logln(InfoLevel, args...)`.
func (logger Logger) Infoln(args ...interface{}) {
	logger.Logln(Info, args...)
}

// Println ensures compatibility with std lib logger.
func (logger Logger) Println(args ...interface{}) {
	logger.Infoln(args...)
}

// Warnln is a convenience method equivalent to `Logln(WarnLevel, args...)`.
func (logger Logger) Warnln(args ...interface{}) {
	logger.Logln(Warn, args...)
}

// Errorln is a convenience method equivalent to `Logln(ErrorLevel, args...)`.
func (logger Logger) Errorln(args ...interface{}) {
	logger.Logln(Error, args...)
}

// Fatalln is a convenience method equivalent to `Logln(FatalLevel, args...)`
// followed by a call to os.Exit(1).
func (logger Logger) Fatalln(args ...interface{}) {
	logger.Logln(Fatal, args...)
	logger.logr.exit(1)
}

// Panicln is a convenience method equivalent to `Logln(PanicLevel, args...)`
// followed by a call to panic().
func (logger Logger) Panicln(args ...interface{}) {
	logger.Logln(Panic, args...)
}