-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger_test.go
53 lines (43 loc) · 1.28 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ginlogrus
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func TestNewBuffer(t *testing.T) {
c := getTestContext("boo", "bar", true)
logger := GetCtxLogger(c)
logger.Info("now")
buff := NewBuffer(logger)
logger.Info("hey")
if strings.Contains(buff.String(), "hey") == false || strings.Contains(buff.String(), "entries") == false {
t.Errorf("Expected hey and found %v", buff.String())
}
if strings.Contains(buff.String(), "now") {
t.Errorf("didn't expeect 'now' and got %v", buff.String())
}
c = getTestContext("boo", "bar", false)
logger.Info("now")
}
func getTestContext(hdr string, v string, withAggregateLogger bool) *gin.Context {
buf := new(bytes.Buffer)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "/", buf)
c.Request.Header.Set(hdr, v)
if withAggregateLogger {
aggregateLoggingBuff := NewLogBuffer()
aggregateRequestLogger := &logrus.Logger{
Out: &aggregateLoggingBuff,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
// you have to use this logger for every *logrus.Entry you create
c.Set("aggregate-logger", aggregateRequestLogger)
}
return c
}