blob: 3276372ad380c09e5f39f9a5af403d7950770fda [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
khenaidood948f772021-08-11 17:49:24 -040021 "github.com/coreos/etcd/pkg/logutil"
Stephane Barbarie260a5632019-02-26 16:12:49 -050022
khenaidooac637102019-01-14 15:44:34 -050023 "google.golang.org/grpc/grpclog"
24)
25
khenaidooac637102019-01-14 15:44:34 -050026var (
Stephane Barbarie260a5632019-02-26 16:12:49 -050027 lgMu sync.RWMutex
28 lg logutil.Logger
khenaidooac637102019-01-14 15:44:34 -050029)
30
31type settableLogger struct {
32 l grpclog.LoggerV2
33 mu sync.RWMutex
34}
35
36func init() {
37 // disable client side logs by default
Stephane Barbarie260a5632019-02-26 16:12:49 -050038 lg = &settableLogger{}
khenaidooac637102019-01-14 15:44:34 -050039 SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
40}
41
42// SetLogger sets client-side Logger.
43func SetLogger(l grpclog.LoggerV2) {
Stephane Barbarie260a5632019-02-26 16:12:49 -050044 lgMu.Lock()
45 lg = logutil.NewLogger(l)
khenaidooac637102019-01-14 15:44:34 -050046 // override grpclog so that any changes happen with locking
Stephane Barbarie260a5632019-02-26 16:12:49 -050047 grpclog.SetLoggerV2(lg)
48 lgMu.Unlock()
khenaidooac637102019-01-14 15:44:34 -050049}
50
Stephane Barbarie260a5632019-02-26 16:12:49 -050051// GetLogger returns the current logutil.Logger.
52func GetLogger() logutil.Logger {
53 lgMu.RLock()
54 l := lg
55 lgMu.RUnlock()
khenaidooac637102019-01-14 15:44:34 -050056 return l
57}
58
Stephane Barbarie260a5632019-02-26 16:12:49 -050059// NewLogger returns a new Logger with logutil.Logger.
60func NewLogger(gl grpclog.LoggerV2) logutil.Logger {
khenaidooac637102019-01-14 15:44:34 -050061 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) }
Stephane Barbarie260a5632019-02-26 16:12:49 -050093func (s *settableLogger) Lvl(lvl int) grpclog.LoggerV2 {
khenaidooac637102019-01-14 15:44:34 -050094 s.mu.RLock()
95 l := s.l
96 s.mu.RUnlock()
97 if l.V(lvl) {
98 return s
99 }
Stephane Barbarie260a5632019-02-26 16:12:49 -0500100 return logutil.NewDiscardLogger()
khenaidooac637102019-01-14 15:44:34 -0500101}