forked from tracer0tong/kafkalogrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafkalogrus_test.go
50 lines (37 loc) · 995 Bytes
/
kafkalogrus_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
package kafkalogrus
import (
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestKafkaHook(t *testing.T) {
// Create a new KafkaHook
hook, err := NewKafkaLogrusHook(
"kh",
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel},
&logrus.JSONFormatter{},
[]string{"127.0.0.1:9092"},
"test",
true,
nil)
if err != nil {
t.Errorf("Can not create KafkaHook: %v\n", err)
}
// Create a new logrus.Logger
logger := logrus.New()
// Add hook to logger
logger.Hooks.Add(hook)
t.Logf("logger: %v", logger)
t.Logf("logger.Out: %v", logger.Out)
t.Logf("logger.Formatter: %v", logger.Formatter)
t.Logf("logger.Hooks: %v", logger.Hooks)
t.Logf("logger.Level: %v", logger.Level)
// Add topics
l := logger.WithField("topic", "nondefaulttopic")
l.Debug("This must not be logged")
l.Info("This is an Info msg")
l.Warn("This is a Warn msg")
l.Error("This is an Error msg")
// Ensure log messages were written to Kafka
time.Sleep(time.Second)
}