...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/logging/settable/logsettable.go

Documentation: github.com/grpc-ecosystem/go-grpc-middleware/logging/settable

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

View as plain text