github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/logging/zap/grpclogger.go (about) 1 // Copyright 2017 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package grpc_zap 5 6 import ( 7 "fmt" 8 9 "github.com/hxx258456/ccgo/grpc/grpclog" 10 "go.uber.org/zap" 11 ) 12 13 // ReplaceGrpcLogger sets the given zap.Logger as a gRPC-level logger. 14 // This should be called *before* any other initialization, preferably from init() functions. 15 func ReplaceGrpcLogger(logger *zap.Logger) { 16 zgl := &zapGrpcLogger{logger.With(SystemField, zap.Bool("grpc_log", true))} 17 grpclog.SetLogger(zgl) 18 } 19 20 type zapGrpcLogger struct { 21 logger *zap.Logger 22 } 23 24 func (l *zapGrpcLogger) Fatal(args ...interface{}) { 25 l.logger.Fatal(fmt.Sprint(args...)) 26 } 27 28 func (l *zapGrpcLogger) Fatalf(format string, args ...interface{}) { 29 l.logger.Fatal(fmt.Sprintf(format, args...)) 30 } 31 32 func (l *zapGrpcLogger) Fatalln(args ...interface{}) { 33 l.logger.Fatal(fmt.Sprint(args...)) 34 } 35 36 func (l *zapGrpcLogger) Print(args ...interface{}) { 37 l.logger.Info(fmt.Sprint(args...)) 38 } 39 40 func (l *zapGrpcLogger) Printf(format string, args ...interface{}) { 41 l.logger.Info(fmt.Sprintf(format, args...)) 42 } 43 44 func (l *zapGrpcLogger) Println(args ...interface{}) { 45 l.logger.Info(fmt.Sprint(args...)) 46 }