From ced531341e99935442723f91ef6949326a0f66cc Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 5 Nov 2014 13:49:58 -0500 Subject: [PATCH] Saving off entry string for use in panic as the reader is not seekable and without this the panic string is always empty :scream_cat: --- entry.go | 7 +++++-- examples/basic/basic.go | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/entry.go b/entry.go index a77c4b0..dc639b3 100644 --- a/entry.go +++ b/entry.go @@ -88,10 +88,13 @@ func (entry *Entry) log(level Level, msg string) { entry.Logger.mu.Unlock() } + var panicBuf bytes.Buffer + teeOut := io.TeeReader(reader, &panicBuf) + entry.Logger.mu.Lock() defer entry.Logger.mu.Unlock() - _, err = io.Copy(entry.Logger.Out, reader) + _, err = io.Copy(entry.Logger.Out, teeOut) if err != nil { fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) } @@ -100,7 +103,7 @@ func (entry *Entry) log(level Level, msg string) { // panic() to use in Entry#Panic(), we avoid the allocation by checking // directly here. if level <= PanicLevel { - panic(reader.String()) + panic(panicBuf.String()) } } diff --git a/examples/basic/basic.go b/examples/basic/basic.go index 3594550..a62ba45 100644 --- a/examples/basic/basic.go +++ b/examples/basic/basic.go @@ -12,6 +12,17 @@ func init() { } func main() { + defer func() { + err := recover() + if err != nil { + log.WithFields(logrus.Fields{ + "omg": true, + "err": err, + "number": 100, + }).Fatal("The ice breaks!") + } + }() + log.WithFields(logrus.Fields{ "animal": "walrus", "size": 10, @@ -23,7 +34,7 @@ func main() { }).Warn("The group's number increased tremendously!") log.WithFields(logrus.Fields{ - "omg": true, - "number": 100, - }).Fatal("The ice breaks!") + "animal": "orca", + "size": 9009, + }).Panic("It's over 9000!") }