zap_grpc_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 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
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. "time"
  23. "go.uber.org/zap"
  24. )
  25. func TestNewGRPCLoggerV2(t *testing.T) {
  26. logPath := filepath.Join(os.TempDir(), fmt.Sprintf("test-log-%d", time.Now().UnixNano()))
  27. defer os.RemoveAll(logPath)
  28. lcfg := zap.Config{
  29. Level: zap.NewAtomicLevelAt(zap.InfoLevel),
  30. Development: false,
  31. Sampling: &zap.SamplingConfig{
  32. Initial: 100,
  33. Thereafter: 100,
  34. },
  35. Encoding: "json",
  36. EncoderConfig: zap.NewProductionEncoderConfig(),
  37. OutputPaths: []string{logPath},
  38. ErrorOutputPaths: []string{logPath},
  39. }
  40. gl, err := NewGRPCLoggerV2(lcfg)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. // debug level is not enabled,
  45. // so info level gRPC-side logging is discarded
  46. gl.Info("etcd-logutil-1")
  47. data, err := ioutil.ReadFile(logPath)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. if bytes.Contains(data, []byte("etcd-logutil-1")) {
  52. t.Fatalf("unexpected line %q", string(data))
  53. }
  54. gl.Warning("etcd-logutil-2")
  55. data, err = ioutil.ReadFile(logPath)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if !bytes.Contains(data, []byte("etcd-logutil-2")) {
  60. t.Fatalf("can't find data in log %q", string(data))
  61. }
  62. if !bytes.Contains(data, []byte("logutil/zap_grpc_test.go:")) {
  63. t.Fatalf("unexpected caller; %q", string(data))
  64. }
  65. }