logger.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2016 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. "log"
  17. "os"
  18. "sync"
  19. "google.golang.org/grpc/grpclog"
  20. )
  21. type Logger grpclog.Logger
  22. var (
  23. logger settableLogger
  24. )
  25. type settableLogger struct {
  26. l grpclog.Logger
  27. mu sync.RWMutex
  28. }
  29. func init() {
  30. // use go's standard logger by default like grpc
  31. logger.mu.Lock()
  32. logger.l = log.New(os.Stderr, "", log.LstdFlags)
  33. grpclog.SetLogger(&logger)
  34. logger.mu.Unlock()
  35. }
  36. func (s *settableLogger) Set(l Logger) {
  37. s.mu.Lock()
  38. logger.l = l
  39. s.mu.Unlock()
  40. }
  41. func (s *settableLogger) Get() Logger {
  42. s.mu.RLock()
  43. l := logger.l
  44. s.mu.RUnlock()
  45. return l
  46. }
  47. // implement the grpclog.Logger interface
  48. func (s *settableLogger) Fatal(args ...interface{}) { s.Get().Fatal(args...) }
  49. func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.Get().Fatalf(format, args...) }
  50. func (s *settableLogger) Fatalln(args ...interface{}) { s.Get().Fatalln(args...) }
  51. func (s *settableLogger) Print(args ...interface{}) { s.Get().Print(args...) }
  52. func (s *settableLogger) Printf(format string, args ...interface{}) { s.Get().Printf(format, args...) }
  53. func (s *settableLogger) Println(args ...interface{}) { s.Get().Println(args...) }