Saving off entry string for use in panic

as the reader is not seekable and without this the panic string is
always empty 🙀
This commit is contained in:
Dan Buch 2014-11-05 13:49:58 -05:00
parent 6ebb4e7b3c
commit ced531341e
2 changed files with 19 additions and 5 deletions

View File

@ -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())
}
}

View File

@ -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!")
}