Skip to content

Commit

Permalink
hooks: add null logger test
Browse files Browse the repository at this point in the history
  • Loading branch information
sirupsen committed May 12, 2017
1 parent 84459f1 commit 9ea5178
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,23 @@ Logrus has a built in facility for asserting the presence of log messages. This
* a test logger (`test.NewNullLogger`) that just records log messages (and does not output any):

```go
import (
"testing"
import(
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/null"
"github.com/stretchr/testify/assert"
"testing"
)

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

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

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

Expand Down
19 changes: 19 additions & 0 deletions hooks/null/null_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package null

import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)

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

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

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

0 comments on commit 9ea5178

Please sign in to comment.