...

Source file src/github.com/sirupsen/logrus/entry_test.go

Documentation: github.com/sirupsen/logrus

     1  package logrus
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestEntryWithError(t *testing.T) {
    13  
    14  	assert := assert.New(t)
    15  
    16  	defer func() {
    17  		ErrorKey = "error"
    18  	}()
    19  
    20  	err := fmt.Errorf("kaboom at layer %d", 4711)
    21  
    22  	assert.Equal(err, WithError(err).Data["error"])
    23  
    24  	logger := New()
    25  	logger.Out = &bytes.Buffer{}
    26  	entry := NewEntry(logger)
    27  
    28  	assert.Equal(err, entry.WithError(err).Data["error"])
    29  
    30  	ErrorKey = "err"
    31  
    32  	assert.Equal(err, entry.WithError(err).Data["err"])
    33  
    34  }
    35  
    36  func TestEntryPanicln(t *testing.T) {
    37  	errBoom := fmt.Errorf("boom time")
    38  
    39  	defer func() {
    40  		p := recover()
    41  		assert.NotNil(t, p)
    42  
    43  		switch pVal := p.(type) {
    44  		case *Entry:
    45  			assert.Equal(t, "kaboom", pVal.Message)
    46  			assert.Equal(t, errBoom, pVal.Data["err"])
    47  		default:
    48  			t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
    49  		}
    50  	}()
    51  
    52  	logger := New()
    53  	logger.Out = &bytes.Buffer{}
    54  	entry := NewEntry(logger)
    55  	entry.WithField("err", errBoom).Panicln("kaboom")
    56  }
    57  
    58  func TestEntryPanicf(t *testing.T) {
    59  	errBoom := fmt.Errorf("boom again")
    60  
    61  	defer func() {
    62  		p := recover()
    63  		assert.NotNil(t, p)
    64  
    65  		switch pVal := p.(type) {
    66  		case *Entry:
    67  			assert.Equal(t, "kaboom true", pVal.Message)
    68  			assert.Equal(t, errBoom, pVal.Data["err"])
    69  		default:
    70  			t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
    71  		}
    72  	}()
    73  
    74  	logger := New()
    75  	logger.Out = &bytes.Buffer{}
    76  	entry := NewEntry(logger)
    77  	entry.WithField("err", errBoom).Panicf("kaboom %v", true)
    78  }
    79  
    80  const (
    81  	badMessage   = "this is going to panic"
    82  	panicMessage = "this is broken"
    83  )
    84  
    85  type panickyHook struct{}
    86  
    87  func (p *panickyHook) Levels() []Level {
    88  	return []Level{InfoLevel}
    89  }
    90  
    91  func (p *panickyHook) Fire(entry *Entry) error {
    92  	if entry.Message == badMessage {
    93  		panic(panicMessage)
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func TestEntryHooksPanic(t *testing.T) {
   100  	logger := New()
   101  	logger.Out = &bytes.Buffer{}
   102  	logger.Level = InfoLevel
   103  	logger.Hooks.Add(&panickyHook{})
   104  
   105  	defer func() {
   106  		p := recover()
   107  		assert.NotNil(t, p)
   108  		assert.Equal(t, panicMessage, p)
   109  
   110  		entry := NewEntry(logger)
   111  		entry.Info("another message")
   112  	}()
   113  
   114  	entry := NewEntry(logger)
   115  	entry.Info(badMessage)
   116  }
   117  
   118  func TestEntryWithIncorrectField(t *testing.T) {
   119  	assert := assert.New(t)
   120  
   121  	fn := func() {}
   122  
   123  	e := Entry{}
   124  	eWithFunc := e.WithFields(Fields{"func": fn})
   125  	eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
   126  
   127  	assert.Equal(eWithFunc.err, `can not add field "func"`)
   128  	assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
   129  
   130  	eWithFunc = eWithFunc.WithField("not_a_func", "it is a string")
   131  	eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string")
   132  
   133  	assert.Equal(eWithFunc.err, `can not add field "func"`)
   134  	assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
   135  
   136  	eWithFunc = eWithFunc.WithTime(time.Now())
   137  	eWithFuncPtr = eWithFuncPtr.WithTime(time.Now())
   138  
   139  	assert.Equal(eWithFunc.err, `can not add field "func"`)
   140  	assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
   141  }
   142  

View as plain text