logger_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "strings"
  19. "testing"
  20. "google.golang.org/grpc/grpclog"
  21. )
  22. func TestLogger(t *testing.T) {
  23. buf := new(bytes.Buffer)
  24. l := NewLogger(grpclog.NewLoggerV2WithVerbosity(buf, buf, buf, 10))
  25. l.Infof("hello world!")
  26. if !strings.Contains(buf.String(), "hello world!") {
  27. t.Fatalf("expected 'hello world!', got %q", buf.String())
  28. }
  29. buf.Reset()
  30. l.Lvl(10).Infof("Level 10")
  31. l.Lvl(30).Infof("Level 30")
  32. if !strings.Contains(buf.String(), "Level 10") {
  33. t.Fatalf("expected 'Level 10', got %q", buf.String())
  34. }
  35. if strings.Contains(buf.String(), "Level 30") {
  36. t.Fatalf("unexpected 'Level 30', got %q", buf.String())
  37. }
  38. buf.Reset()
  39. l = NewLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
  40. l.Infof("ignore this")
  41. if len(buf.Bytes()) > 0 {
  42. t.Fatalf("unexpected logs %q", buf.String())
  43. }
  44. }