github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/swarmkit/log/grpc.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"google.golang.org/grpc/grpclog"
     8  )
     9  
    10  type logrusWrapper struct {
    11  	*logrus.Entry
    12  }
    13  
    14  // V provides the functionality that returns whether a particular log level is at
    15  // least l - this is needed to meet the LoggerV2 interface.  GRPC's logging levels
    16  // are: https://github.com/grpc/grpc-go/blob/master/grpclog/loggerv2.go#L71
    17  // 0=info, 1=warning, 2=error, 3=fatal
    18  // logrus's are: https://github.com/sirupsen/logrus/blob/master/logrus.go
    19  // 0=panic, 1=fatal, 2=error, 3=warn, 4=info, 5=debug
    20  func (lw logrusWrapper) V(l int) bool {
    21  	// translate to logrus level
    22  	logrusLevel := 4 - l
    23  	return int(lw.Logger.Level) <= logrusLevel
    24  }
    25  
    26  func init() {
    27  	ctx := WithModule(context.Background(), "grpc")
    28  
    29  	// completely replace the grpc logger with the logrus logger.
    30  	grpclog.SetLoggerV2(logrusWrapper{Entry: G(ctx)})
    31  }