forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogsettable_test.go
48 lines (36 loc) · 1.27 KB
/
logsettable_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
package grpc_logsettable_test
import (
"bytes"
"io/ioutil"
"os"
"testing"
grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/grpclog"
)
func ExampleSettableLoggerV2_init() {
l1 := grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard)
l2 := grpclog.NewLoggerV2(os.Stdout, os.Stdout, os.Stdout)
settableLogger := grpc_logsettable.ReplaceGrpcLoggerV2()
grpclog.Info("Discarded by default")
settableLogger.Set(l1)
grpclog.Info("Discarded log by l1")
settableLogger.Set(l2)
grpclog.Info("Emitted log by l2")
// Expected output: INFO: 2021/03/15 12:59:54 [Emitted log by l2]
}
func TestSettableLoggerV2_init(t *testing.T) {
l1buffer := &bytes.Buffer{}
l1 := grpclog.NewLoggerV2(l1buffer, l1buffer, l1buffer)
l2buffer := &bytes.Buffer{}
l2 := grpclog.NewLoggerV2(l2buffer, l2buffer, l2buffer)
settableLogger := grpc_logsettable.ReplaceGrpcLoggerV2()
grpclog.Info("Discarded by default")
settableLogger.Set(l1)
grpclog.SetLoggerV2(settableLogger)
grpclog.Info("Emitted log by l1")
settableLogger.Set(l2)
grpclog.Info("Emitted log by l2")
assert.Contains(t, l1buffer.String(), "Emitted log by l1")
assert.Contains(t, l2buffer.String(), "Emitted log by l2")
}