blob: 782e313137a6c8431b6fa5d409ce887cc1d9849e [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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
15package clientv3
16
17import (
18 "io/ioutil"
19 "sync"
20
21 "google.golang.org/grpc/grpclog"
22)
23
24// Logger is the logger used by client library.
25// It implements grpclog.LoggerV2 interface.
26type Logger interface {
27 grpclog.LoggerV2
28
29 // Lvl returns logger if logger's verbosity level >= "lvl".
30 // Otherwise, logger that discards all logs.
31 Lvl(lvl int) Logger
32
33 // to satisfy capnslog
34
35 Print(args ...interface{})
36 Printf(format string, args ...interface{})
37 Println(args ...interface{})
38}
39
40var (
41 loggerMu sync.RWMutex
42 logger Logger
43)
44
45type settableLogger struct {
46 l grpclog.LoggerV2
47 mu sync.RWMutex
48}
49
50func init() {
51 // disable client side logs by default
52 logger = &settableLogger{}
53 SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
54}
55
56// SetLogger sets client-side Logger.
57func SetLogger(l grpclog.LoggerV2) {
58 loggerMu.Lock()
59 logger = NewLogger(l)
60 // override grpclog so that any changes happen with locking
61 grpclog.SetLoggerV2(logger)
62 loggerMu.Unlock()
63}
64
65// GetLogger returns the current logger.
66func GetLogger() Logger {
67 loggerMu.RLock()
68 l := logger
69 loggerMu.RUnlock()
70 return l
71}
72
73// NewLogger returns a new Logger with grpclog.LoggerV2.
74func NewLogger(gl grpclog.LoggerV2) Logger {
75 return &settableLogger{l: gl}
76}
77
78func (s *settableLogger) get() grpclog.LoggerV2 {
79 s.mu.RLock()
80 l := s.l
81 s.mu.RUnlock()
82 return l
83}
84
85// implement the grpclog.LoggerV2 interface
86
87func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
88func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
89func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
90func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
91func (s *settableLogger) Warningf(format string, args ...interface{}) {
92 s.get().Warningf(format, args...)
93}
94func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
95func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
96func (s *settableLogger) Errorf(format string, args ...interface{}) {
97 s.get().Errorf(format, args...)
98}
99func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
100func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
101func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
102func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
103func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
104func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
105func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
106func (s *settableLogger) V(l int) bool { return s.get().V(l) }
107func (s *settableLogger) Lvl(lvl int) Logger {
108 s.mu.RLock()
109 l := s.l
110 s.mu.RUnlock()
111 if l.V(lvl) {
112 return s
113 }
114 return &noLogger{}
115}
116
117type noLogger struct{}
118
119func (*noLogger) Info(args ...interface{}) {}
120func (*noLogger) Infof(format string, args ...interface{}) {}
121func (*noLogger) Infoln(args ...interface{}) {}
122func (*noLogger) Warning(args ...interface{}) {}
123func (*noLogger) Warningf(format string, args ...interface{}) {}
124func (*noLogger) Warningln(args ...interface{}) {}
125func (*noLogger) Error(args ...interface{}) {}
126func (*noLogger) Errorf(format string, args ...interface{}) {}
127func (*noLogger) Errorln(args ...interface{}) {}
128func (*noLogger) Fatal(args ...interface{}) {}
129func (*noLogger) Fatalf(format string, args ...interface{}) {}
130func (*noLogger) Fatalln(args ...interface{}) {}
131func (*noLogger) Print(args ...interface{}) {}
132func (*noLogger) Printf(format string, args ...interface{}) {}
133func (*noLogger) Println(args ...interface{}) {}
134func (*noLogger) V(l int) bool { return false }
135func (ng *noLogger) Lvl(lvl int) Logger { return ng }