blob: 9e403b2b2d023aa1413e4fa868b3f6124fe8b3e7 [file] [log] [blame]
Abhay Kumara61c5222025-11-10 07:32:50 +00001package grpc_logsettable
2
3import (
4 "io/ioutil"
5 "sync"
6
7 "google.golang.org/grpc/grpclog"
8)
9
10// SettableLoggerV2 is thread-safe.
11type SettableLoggerV2 interface {
12 grpclog.LoggerV2
13 // Sets given logger as the underlying implementation.
14 Set(loggerv2 grpclog.LoggerV2)
15 // Sets `discard` logger as the underlying implementation.
16 Reset()
17}
18
19// ReplaceGrpcLoggerV2 creates and configures SettableLoggerV2 as grpc logger.
20func ReplaceGrpcLoggerV2() SettableLoggerV2 {
21 settable := &settableLoggerV2{}
22 settable.Reset()
23 grpclog.SetLoggerV2(settable)
24 return settable
25}
26
27// SettableLoggerV2 implements SettableLoggerV2
28type settableLoggerV2 struct {
29 log grpclog.LoggerV2
30 mu sync.RWMutex
31}
32
33func (s *settableLoggerV2) Set(log grpclog.LoggerV2) {
34 s.mu.Lock()
35 defer s.mu.Unlock()
36 s.log = log
37}
38
39func (s *settableLoggerV2) Reset() {
40 s.Set(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
41}
42
43func (s *settableLoggerV2) get() grpclog.LoggerV2 {
44 s.mu.RLock()
45 defer s.mu.RUnlock()
46 return s.log
47}
48
49func (s *settableLoggerV2) Info(args ...interface{}) {
50 s.get().Info(args)
51}
52
53func (s *settableLoggerV2) Infoln(args ...interface{}) {
54 s.get().Infoln(args)
55}
56
57func (s *settableLoggerV2) Infof(format string, args ...interface{}) {
58 s.get().Infof(format, args)
59}
60
61func (s *settableLoggerV2) Warning(args ...interface{}) {
62 s.get().Warning(args)
63}
64
65func (s *settableLoggerV2) Warningln(args ...interface{}) {
66 s.get().Warningln(args)
67}
68
69func (s *settableLoggerV2) Warningf(format string, args ...interface{}) {
70 s.get().Warningf(format, args)
71}
72
73func (s *settableLoggerV2) Error(args ...interface{}) {
74 s.get().Error(args)
75}
76
77func (s *settableLoggerV2) Errorln(args ...interface{}) {
78 s.get().Errorln(args)
79}
80
81func (s *settableLoggerV2) Errorf(format string, args ...interface{}) {
82 s.get().Errorf(format, args)
83}
84
85func (s *settableLoggerV2) Fatal(args ...interface{}) {
86 s.get().Fatal(args)
87}
88
89func (s *settableLoggerV2) Fatalln(args ...interface{}) {
90 s.get().Fatalln(args)
91}
92
93func (s *settableLoggerV2) Fatalf(format string, args ...interface{}) {
94 s.get().Fatalf(format, args)
95}
96
97func (s *settableLoggerV2) V(l int) bool {
98 return s.get().V(l)
99}