logger_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 logutil_test
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "strings"
  19. "testing"
  20. "github.com/coreos/etcd/pkg/logutil"
  21. "google.golang.org/grpc/grpclog"
  22. )
  23. func TestLogger(t *testing.T) {
  24. buf := new(bytes.Buffer)
  25. l := logutil.NewLogger(grpclog.NewLoggerV2WithVerbosity(buf, buf, buf, 10))
  26. l.Infof("hello world!")
  27. if !strings.Contains(buf.String(), "hello world!") {
  28. t.Fatalf("expected 'hello world!', got %q", buf.String())
  29. }
  30. buf.Reset()
  31. l.Lvl(10).Infof("Level 10")
  32. l.Lvl(30).Infof("Level 30")
  33. if !strings.Contains(buf.String(), "Level 10") {
  34. t.Fatalf("expected 'Level 10', got %q", buf.String())
  35. }
  36. if strings.Contains(buf.String(), "Level 30") {
  37. t.Fatalf("unexpected 'Level 30', got %q", buf.String())
  38. }
  39. buf.Reset()
  40. l = logutil.NewLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
  41. l.Infof("ignore this")
  42. if len(buf.Bytes()) > 0 {
  43. t.Fatalf("unexpected logs %q", buf.String())
  44. }
  45. }