21
minimumCallerDepth int
24
callerInitOnce sync.Once
28
maximumCallerDepth int = 25
29
knownLogrusFrames int = 4
34
minimumCallerDepth = 1
67
Context context.Context
73
func NewEntry(logger *Logger) *Entry {
77
Data: make(Fields, 6),
81
func (entry *Entry) Dup() *Entry {
82
data := make(Fields, len(entry.Data))
83
for k, v := range entry.Data {
86
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, Context: entry.Context, err: entry.err}
90
func (entry *Entry) Bytes() ([]byte, error) {
91
return entry.Logger.Formatter.Format(entry)
96
func (entry *Entry) String() (string, error) {
97
serialized, err := entry.Bytes()
101
str := string(serialized)
106
func (entry *Entry) WithError(err error) *Entry {
107
return entry.WithField(ErrorKey, err)
111
func (entry *Entry) WithContext(ctx context.Context) *Entry {
112
dataCopy := make(Fields, len(entry.Data))
113
for k, v := range entry.Data {
116
return &Entry{Logger: entry.Logger, Data: dataCopy, Time: entry.Time, err: entry.err, Context: ctx}
120
func (entry *Entry) WithField(key string, value interface{}) *Entry {
121
return entry.WithFields(Fields{key: value})
125
func (entry *Entry) WithFields(fields Fields) *Entry {
126
data := make(Fields, len(entry.Data)+len(fields))
127
for k, v := range entry.Data {
130
fieldErr := entry.err
131
for k, v := range fields {
133
if t := reflect.TypeOf(v); t != nil {
135
case t.Kind() == reflect.Func, t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func:
140
tmp := fmt.Sprintf("can not add field %q", k)
142
fieldErr = entry.err + ", " + tmp
150
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}
154
func (entry *Entry) WithTime(t time.Time) *Entry {
155
dataCopy := make(Fields, len(entry.Data))
156
for k, v := range entry.Data {
159
return &Entry{Logger: entry.Logger, Data: dataCopy, Time: t, err: entry.err, Context: entry.Context}
164
func getPackageName(f string) string {
166
lastPeriod := strings.LastIndex(f, ".")
167
lastSlash := strings.LastIndex(f, "/")
168
if lastPeriod > lastSlash {
179
func getCaller() *runtime.Frame {
181
callerInitOnce.Do(func() {
182
pcs := make([]uintptr, maximumCallerDepth)
183
_ = runtime.Callers(0, pcs)
186
for i := 0; i < maximumCallerDepth; i++ {
187
funcName := runtime.FuncForPC(pcs[i]).Name()
188
if strings.Contains(funcName, "getCaller") {
189
logrusPackage = getPackageName(funcName)
194
minimumCallerDepth = knownLogrusFrames
198
pcs := make([]uintptr, maximumCallerDepth)
199
depth := runtime.Callers(minimumCallerDepth, pcs)
200
frames := runtime.CallersFrames(pcs[:depth])
202
for f, again := frames.Next(); again; f, again = frames.Next() {
203
pkg := getPackageName(f.Function)
206
if pkg != logrusPackage {
215
func (entry Entry) HasCaller() (has bool) {
216
return entry.Logger != nil &&
217
entry.Logger.ReportCaller &&
221
func (entry *Entry) log(level Level, msg string) {
222
var buffer *bytes.Buffer
224
newEntry := entry.Dup()
226
if newEntry.Time.IsZero() {
227
newEntry.Time = time.Now()
230
newEntry.Level = level
231
newEntry.Message = msg
233
newEntry.Logger.mu.Lock()
234
reportCaller := newEntry.Logger.ReportCaller
235
newEntry.Logger.mu.Unlock()
238
newEntry.Caller = getCaller()
245
newEntry.Buffer = nil
249
newEntry.Buffer = buffer
253
newEntry.Buffer = nil
258
if level <= PanicLevel {
263
func (entry *Entry) fireHooks() {
264
var tmpHooks LevelHooks
265
entry.Logger.mu.Lock()
266
tmpHooks = make(LevelHooks, len(entry.Logger.Hooks))
267
for k, v := range entry.Logger.Hooks {
270
entry.Logger.mu.Unlock()
272
err := tmpHooks.Fire(entry.Level, entry)
274
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
278
func (entry *Entry) write() {
279
serialized, err := entry.Logger.Formatter.Format(entry)
281
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
284
entry.Logger.mu.Lock()
285
defer entry.Logger.mu.Unlock()
286
if _, err := entry.Logger.Out.Write(serialized); err != nil {
287
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
291
func (entry *Entry) Log(level Level, args ...interface{}) {
292
if entry.Logger.IsLevelEnabled(level) {
293
entry.log(level, fmt.Sprint(args...))
297
func (entry *Entry) Trace(args ...interface{}) {
298
entry.Log(TraceLevel, args...)
301
func (entry *Entry) Debug(args ...interface{}) {
302
entry.Log(DebugLevel, args...)
305
func (entry *Entry) Print(args ...interface{}) {
309
func (entry *Entry) Info(args ...interface{}) {
310
entry.Log(InfoLevel, args...)
313
func (entry *Entry) Warn(args ...interface{}) {
314
entry.Log(WarnLevel, args...)
317
func (entry *Entry) Warning(args ...interface{}) {
321
func (entry *Entry) Error(args ...interface{}) {
322
entry.Log(ErrorLevel, args...)
325
func (entry *Entry) Fatal(args ...interface{}) {
326
entry.Log(FatalLevel, args...)
330
func (entry *Entry) Panic(args ...interface{}) {
331
entry.Log(PanicLevel, args...)
336
func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
337
if entry.Logger.IsLevelEnabled(level) {
338
entry.Log(level, fmt.Sprintf(format, args...))
342
func (entry *Entry) Tracef(format string, args ...interface{}) {
343
entry.Logf(TraceLevel, format, args...)
346
func (entry *Entry) Debugf(format string, args ...interface{}) {
347
entry.Logf(DebugLevel, format, args...)
350
func (entry *Entry) Infof(format string, args ...interface{}) {
351
entry.Logf(InfoLevel, format, args...)
354
func (entry *Entry) Printf(format string, args ...interface{}) {
355
entry.Infof(format, args...)
358
func (entry *Entry) Warnf(format string, args ...interface{}) {
359
entry.Logf(WarnLevel, format, args...)
362
func (entry *Entry) Warningf(format string, args ...interface{}) {
363
entry.Warnf(format, args...)
366
func (entry *Entry) Errorf(format string, args ...interface{}) {
367
entry.Logf(ErrorLevel, format, args...)
370
func (entry *Entry) Fatalf(format string, args ...interface{}) {
371
entry.Logf(FatalLevel, format, args...)
375
func (entry *Entry) Panicf(format string, args ...interface{}) {
376
entry.Logf(PanicLevel, format, args...)
381
func (entry *Entry) Logln(level Level, args ...interface{}) {
382
if entry.Logger.IsLevelEnabled(level) {
383
entry.Log(level, entry.sprintlnn(args...))
387
func (entry *Entry) Traceln(args ...interface{}) {
388
entry.Logln(TraceLevel, args...)
391
func (entry *Entry) Debugln(args ...interface{}) {
392
entry.Logln(DebugLevel, args...)
395
func (entry *Entry) Infoln(args ...interface{}) {
396
entry.Logln(InfoLevel, args...)
399
func (entry *Entry) Println(args ...interface{}) {
400
entry.Infoln(args...)
403
func (entry *Entry) Warnln(args ...interface{}) {
404
entry.Logln(WarnLevel, args...)
407
func (entry *Entry) Warningln(args ...interface{}) {
408
entry.Warnln(args...)
411
func (entry *Entry) Errorln(args ...interface{}) {
412
entry.Logln(ErrorLevel, args...)
415
func (entry *Entry) Fatalln(args ...interface{}) {
416
entry.Logln(FatalLevel, args...)
420
func (entry *Entry) Panicln(args ...interface{}) {
421
entry.Logln(PanicLevel, args...)
428
func (entry *Entry) sprintlnn(args ...interface{}) string {
429
msg := fmt.Sprintln(args...)
430
return msg[:len(msg)-1]