Debian Codegraph @

vendor/github.com/sirupsen/logrus/entry.go

raw
1 package logrus
2
3 import (
4 "bytes"
5 "fmt"
6 "os"
7 "sync"
8 "time"
9 )
10
11 var bufferPool *sync.Pool
12
13 func init() {
14 bufferPool = &sync.Pool{
15 New: func() interface{} {
16 return new(bytes.Buffer)
17 },
18 }
19 }
20
21 // Defines the key when adding errors using WithError.
22 var ErrorKey = "error"
23
24 // An entry is the final or intermediate Logrus logging entry. It contains all
25 // the fields passed with WithField{,s}. It's finally logged when Debug, Info,
26 // Warn, Error, Fatal or Panic is called on it. These objects can be reused and
27 // passed around as much as you wish to avoid field duplication.
28 type Entry struct {
29 Logger *Logger
30
31 // Contains all the fields set by the user.
32 Data Fields
33
34 // Time at which the log entry was created
35 Time time.Time
36
37 // Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic
38 // This field will be set on entry firing and the value will be equal to the one in Logger struct field.
39 Level Level
40
41 // Message passed to Debug, Info, Warn, Error, Fatal or Panic
42 Message string
43
44 // When formatter is called in entry.log(), an Buffer may be set to entry
45 Buffer *bytes.Buffer
46 }
47
48 func NewEntry(logger *Logger) *Entry {
49 return &Entry{
50 Logger: logger,
51 // Default is five fields, give a little extra room
52 Data: make(Fields, 5),
53 }
54 }
55
56 // Returns the string representation from the reader and ultimately the
57 // formatter.
58 func (entry *Entry) String() (string, error) {
59 serialized, err := entry.Logger.Formatter.Format(entry)
60 if err != nil {
61 return "", err
62 }
63 str := string(serialized)
64 return str, nil
65 }
66
67 // Add an error as single field (using the key defined in ErrorKey) to the Entry.
68 func (entry *Entry) WithError(err error) *Entry {
69 return entry.WithField(ErrorKey, err)
70 }
71
72 // Add a single field to the Entry.
73 func (entry *Entry) WithField(key string, value interface{}) *Entry {
74 return entry.WithFields(Fields{key: value})
75 }
76
77 // Add a map of fields to the Entry.
78 func (entry *Entry) WithFields(fields Fields) *Entry {
79 data := make(Fields, len(entry.Data)+len(fields))
80 for k, v := range entry.Data {
81 data[k] = v
82 }
83 for k, v := range fields {
84 data[k] = v
85 }
86 return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time}
87 }
88
89 // Overrides the time of the Entry.
90 func (entry *Entry) WithTime(t time.Time) *Entry {
91 return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
92 }
93
94 // This function is not declared with a pointer value because otherwise
95 // race conditions will occur when using multiple goroutines
96 func (entry Entry) log(level Level, msg string) {
97 var buffer *bytes.Buffer
98
99 // Default to now, but allow users to override if they want.
100 //
101 // We don't have to worry about polluting future calls to Entry#log()
102 // with this assignment because this function is declared with a
103 // non-pointer receiver.
104 if entry.Time.IsZero() {
105 entry.Time = time.Now()
106 }
107
108 entry.Level = level
109 entry.Message = msg
110
111 entry.fireHooks()
112
113 buffer = bufferPool.Get().(*bytes.Buffer)
114 buffer.Reset()
115 defer bufferPool.Put(buffer)
116 entry.Buffer = buffer
117
118 entry.write()
119
120 entry.Buffer = nil
121
122 // To avoid Entry#log() returning a value that only would make sense for
123 // panic() to use in Entry#Panic(), we avoid the allocation by checking
124 // directly here.
125 if level <= PanicLevel {
126 panic(&entry)
127 }
128 }
129
130 func (entry *Entry) fireHooks() {
131 entry.Logger.mu.Lock()
132 defer entry.Logger.mu.Unlock()
133 err := entry.Logger.Hooks.Fire(entry.Level, entry)
134 if err != nil {
135 fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
136 }
137 }
138
139 func (entry *Entry) write() {
140 serialized, err := entry.Logger.Formatter.Format(entry)
141 entry.Logger.mu.Lock()
142 defer entry.Logger.mu.Unlock()
143 if err != nil {
144 fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
145 } else {
146 _, err = entry.Logger.Out.Write(serialized)
147 if err != nil {
148 fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
149 }
150 }
151 }
152
153 func (entry *Entry) Debug(args ...interface{}) {
154 if entry.Logger.level() >= DebugLevel {
155 entry.log(DebugLevel, fmt.Sprint(args...))
156 }
157 }
158
159 func (entry *Entry) Print(args ...interface{}) {
160 entry.Info(args...)
161 }
162
163 func (entry *Entry) Info(args ...interface{}) {
164 if entry.Logger.level() >= InfoLevel {
165 entry.log(InfoLevel, fmt.Sprint(args...))
166 }
167 }
168
169 func (entry *Entry) Warn(args ...interface{}) {
170 if entry.Logger.level() >= WarnLevel {
171 entry.log(WarnLevel, fmt.Sprint(args...))
172 }
173 }
174
175 func (entry *Entry) Warning(args ...interface{}) {
176 entry.Warn(args...)
177 }
178
179 func (entry *Entry) Error(args ...interface{}) {
180 if entry.Logger.level() >= ErrorLevel {
181 entry.log(ErrorLevel, fmt.Sprint(args...))
182 }
183 }
184
185 func (entry *Entry) Fatal(args ...interface{}) {
186 if entry.Logger.level() >= FatalLevel {
187 entry.log(FatalLevel, fmt.Sprint(args...))
188 }
189 Exit(1)
190 }
191
192 func (entry *Entry) Panic(args ...interface{}) {
193 if entry.Logger.level() >= PanicLevel {
194 entry.log(PanicLevel, fmt.Sprint(args...))
195 }
196 panic(fmt.Sprint(args...))
197 }
198
199 // Entry Printf family functions
200
201 func (entry *Entry) Debugf(format string, args ...interface{}) {
202 if entry.Logger.level() >= DebugLevel {
203 entry.Debug(fmt.Sprintf(format, args...))
204 }
205 }
206
207 func (entry *Entry) Infof(format string, args ...interface{}) {
208 if entry.Logger.level() >= InfoLevel {
209 entry.Info(fmt.Sprintf(format, args...))
210 }
211 }
212
213 func (entry *Entry) Printf(format string, args ...interface{}) {
214 entry.Infof(format, args...)
215 }
216
217 func (entry *Entry) Warnf(format string, args ...interface{}) {
218 if entry.Logger.level() >= WarnLevel {
219 entry.Warn(fmt.Sprintf(format, args...))
220 }
221 }
222
223 func (entry *Entry) Warningf(format string, args ...interface{}) {
224 entry.Warnf(format, args...)
225 }
226
227 func (entry *Entry) Errorf(format string, args ...interface{}) {
228 if entry.Logger.level() >= ErrorLevel {
229 entry.Error(fmt.Sprintf(format, args...))
230 }
231 }
232
233 func (entry *Entry) Fatalf(format string, args ...interface{}) {
234 if entry.Logger.level() >= FatalLevel {
235 entry.Fatal(fmt.Sprintf(format, args...))
236 }
237 Exit(1)
238 }
239
240 func (entry *Entry) Panicf(format string, args ...interface{}) {
241 if entry.Logger.level() >= PanicLevel {
242 entry.Panic(fmt.Sprintf(format, args...))
243 }
244 }
245
246 // Entry Println family functions
247
248 func (entry *Entry) Debugln(args ...interface{}) {
249 if entry.Logger.level() >= DebugLevel {
250 entry.Debug(entry.sprintlnn(args...))
251 }
252 }
253
254 func (entry *Entry) Infoln(args ...interface{}) {
255 if entry.Logger.level() >= InfoLevel {
256 entry.Info(entry.sprintlnn(args...))
257 }
258 }
259
260 func (entry *Entry) Println(args ...interface{}) {
261 entry.Infoln(args...)
262 }
263
264 func (entry *Entry) Warnln(args ...interface{}) {
265 if entry.Logger.level() >= WarnLevel {
266 entry.Warn(entry.sprintlnn(args...))
267 }
268 }
269
270 func (entry *Entry) Warningln(args ...interface{}) {
271 entry.Warnln(args...)
272 }
273
274 func (entry *Entry) Errorln(args ...interface{}) {
275 if entry.Logger.level() >= ErrorLevel {
276 entry.Error(entry.sprintlnn(args...))
277 }
278 }
279
280 func (entry *Entry) Fatalln(args ...interface{}) {
281 if entry.Logger.level() >= FatalLevel {
282 entry.Fatal(entry.sprintlnn(args...))
283 }
284 Exit(1)
285 }
286
287 func (entry *Entry) Panicln(args ...interface{}) {
288 if entry.Logger.level() >= PanicLevel {
289 entry.Panic(entry.sprintlnn(args...))
290 }
291 }
292
293 // Sprintlnn => Sprint no newline. This is to get the behavior of how
294 // fmt.Sprintln where spaces are always added between operands, regardless of
295 // their type. Instead of vendoring the Sprintln implementation to spare a
296 // string allocation, we do the simplest thing.
297 func (entry *Entry) sprintlnn(args ...interface{}) string {
298 msg := fmt.Sprintln(args...)
299 return msg[:len(msg)-1]
300 }