package_logger_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. "go.etcd.io/etcd/pkg/logutil"
  21. "github.com/coreos/pkg/capnslog"
  22. )
  23. func TestPackageLogger(t *testing.T) {
  24. buf := new(bytes.Buffer)
  25. capnslog.SetFormatter(capnslog.NewDefaultFormatter(buf))
  26. l := logutil.NewPackageLogger("go.etcd.io/etcd", "logger")
  27. r := capnslog.MustRepoLogger("go.etcd.io/etcd")
  28. r.SetLogLevel(map[string]capnslog.LogLevel{"logger": capnslog.INFO})
  29. l.Infof("hello world!")
  30. if !strings.Contains(buf.String(), "hello world!") {
  31. t.Fatalf("expected 'hello world!', got %q", buf.String())
  32. }
  33. buf.Reset()
  34. // capnslog.INFO is 3
  35. l.Lvl(2).Infof("Level 2")
  36. l.Lvl(5).Infof("Level 5")
  37. if !strings.Contains(buf.String(), "Level 2") {
  38. t.Fatalf("expected 'Level 2', got %q", buf.String())
  39. }
  40. if strings.Contains(buf.String(), "Level 5") {
  41. t.Fatalf("unexpected 'Level 5', got %q", buf.String())
  42. }
  43. buf.Reset()
  44. capnslog.SetFormatter(capnslog.NewDefaultFormatter(ioutil.Discard))
  45. l.Infof("ignore this")
  46. if len(buf.Bytes()) > 0 {
  47. t.Fatalf("unexpected logs %q", buf.String())
  48. }
  49. }