From 95190bb5ae0af6be6a911f807a33b291a7e9cdf4 Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Fri, 9 Oct 2015 16:07:29 +0200 Subject: [PATCH] Added testing section to README. --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2aea0e..2119b04 100644 --- a/README.md +++ b/README.md @@ -365,4 +365,21 @@ entries. It should not be a feature of the application-level logger. | ---- | ----------- | |[Logrus Mate](https://github.com/gogap/logrus_mate)|Logrus mate is a tool for Logrus to manage loggers, you can initial logger's level, hook and formatter by config file, the logger will generated with different config at different environment.| -[godoc]: https://godoc.org/github.com/Sirupsen/logrus +#### Testing + +Logrus 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): + +```go +logger, hook := NewNullLogger() +logger.Error("Hello error") + +assert.Equal(1, len(hook.Entries)) +assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level) +assert.Equal("Hello error", hook.LastEntry().Message) + +hook.Reset() +assert.Nil(hook.LastEntry()) +```