Skip to content

Testing

Michael Kenney edited this page Aug 13, 2018 · 1 revision

bdlm/log has a built in facility for asserting the presence of log messages. This is implemented through the test hook and provides:

  • decorators for existing logger (test.NewLocal and test.NewGlobal) which basically just add the test hook
  • a test logger (test.NewNullLogger) that just records log messages (and does not output any):
import(
    "github.com/bdlm/log"
    "github.com/bdlm/log/hooks/test"
    "github.com/stretchr/testify/assert"
    "testing"
)

func TestSomething(t*testing.T){
    logger, hook := test.NewNullLogger()
    logger.Error("Helloerror")

    assert.Equal(t, 1, len(hook.Entries))
    assert.Equal(t, log.ErrorLevel, hook.LastEntry().Level)
    assert.Equal(t, "Helloerror", hook.LastEntry().Message)

    hook.Reset()
    assert.Nil(t, hook.LastEntry())
}

Clone this wiki locally