Adding tests for PanicLevel behavior, plus changing behavior! 🙀

This commit is contained in:
Dan Buch 2014-11-05 20:39:35 -05:00
parent f302a46d2a
commit 9836f1ba0e
2 changed files with 55 additions and 4 deletions

View File

@ -71,8 +71,6 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
}
func (entry *Entry) log(level Level, msg string) {
var panicBuf bytes.Buffer
entry.Time = time.Now()
entry.Level = level
entry.Message = msg
@ -93,7 +91,7 @@ func (entry *Entry) log(level Level, msg string) {
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
_, err = io.Copy(io.MultiWriter(entry.Logger.Out, &panicBuf), reader)
_, err = io.Copy(entry.Logger.Out, reader)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
}
@ -102,7 +100,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(panicBuf.String())
panic(entry)
}
}

53
entry_test.go Normal file
View File

@ -0,0 +1,53 @@
package logrus
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEntryPanicln(t *testing.T) {
errBoom := fmt.Errorf("boom time")
defer func() {
p := recover()
assert.NotNil(t, p)
switch pVal := p.(type) {
case *Entry:
assert.Equal(t, "kaboom", pVal.Message)
assert.Equal(t, errBoom, pVal.Data["err"])
default:
t.Fatal()
}
}()
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
entry.WithField("err", errBoom).Panicln("kaboom")
}
func TestEntryPanicf(t *testing.T) {
errBoom := fmt.Errorf("boom again")
defer func() {
p := recover()
assert.NotNil(t, p)
switch pVal := p.(type) {
case *Entry:
assert.Equal(t, "kaboom true", pVal.Message)
assert.Equal(t, errBoom, pVal.Data["err"])
default:
t.Fatal()
}
}()
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
entry.WithField("err", errBoom).Panicf("kaboom %v", true)
}