blob: 3276372ad380c09e5f39f9a5af403d7950770fda [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// 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 "github.com/coreos/etcd/pkg/logutil"
22
23 "google.golang.org/grpc/grpclog"
24)
25
26var (
27 lgMu sync.RWMutex
28 lg logutil.Logger
29)
30
31type settableLogger struct {
32 l grpclog.LoggerV2
33 mu sync.RWMutex
34}
35
36func init() {
37 // disable client side logs by default
38 lg = &settableLogger{}
39 SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
40}
41
42// SetLogger sets client-side Logger.
43func SetLogger(l grpclog.LoggerV2) {
44 lgMu.Lock()
45 lg = logutil.NewLogger(l)
46 // override grpclog so that any changes happen with locking
47 grpclog.SetLoggerV2(lg)
48 lgMu.Unlock()
49}
50
51// GetLogger returns the current logutil.Logger.
52func GetLogger() logutil.Logger {
53 lgMu.RLock()
54 l := lg
55 lgMu.RUnlock()
56 return l
57}
58
59// NewLogger returns a new Logger with logutil.Logger.
60func NewLogger(gl grpclog.LoggerV2) logutil.Logger {
61 return &settableLogger{l: gl}
62}
63
64func (s *settableLogger) get() grpclog.LoggerV2 {
65 s.mu.RLock()
66 l := s.l
67 s.mu.RUnlock()
68 return l
69}
70
71// implement the grpclog.LoggerV2 interface
72
73func (s *settableLogger) Info(args ...interface{}) { s.get().Info(args...) }
74func (s *settableLogger) Infof(format string, args ...interface{}) { s.get().Infof(format, args...) }
75func (s *settableLogger) Infoln(args ...interface{}) { s.get().Infoln(args...) }
76func (s *settableLogger) Warning(args ...interface{}) { s.get().Warning(args...) }
77func (s *settableLogger) Warningf(format string, args ...interface{}) {
78 s.get().Warningf(format, args...)
79}
80func (s *settableLogger) Warningln(args ...interface{}) { s.get().Warningln(args...) }
81func (s *settableLogger) Error(args ...interface{}) { s.get().Error(args...) }
82func (s *settableLogger) Errorf(format string, args ...interface{}) {
83 s.get().Errorf(format, args...)
84}
85func (s *settableLogger) Errorln(args ...interface{}) { s.get().Errorln(args...) }
86func (s *settableLogger) Fatal(args ...interface{}) { s.get().Fatal(args...) }
87func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.get().Fatalf(format, args...) }
88func (s *settableLogger) Fatalln(args ...interface{}) { s.get().Fatalln(args...) }
89func (s *settableLogger) Print(args ...interface{}) { s.get().Info(args...) }
90func (s *settableLogger) Printf(format string, args ...interface{}) { s.get().Infof(format, args...) }
91func (s *settableLogger) Println(args ...interface{}) { s.get().Infoln(args...) }
92func (s *settableLogger) V(l int) bool { return s.get().V(l) }
93func (s *settableLogger) Lvl(lvl int) grpclog.LoggerV2 {
94 s.mu.RLock()
95 l := s.l
96 s.mu.RUnlock()
97 if l.V(lvl) {
98 return s
99 }
100 return logutil.NewDiscardLogger()
101}