Skip to content

Commit

Permalink
fix: grpc log
Browse files Browse the repository at this point in the history
  • Loading branch information
liov committed Jun 24, 2024
1 parent 185938a commit 0e2e49d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 61 deletions.
2 changes: 1 addition & 1 deletion server/handler_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func (s *Server) grpcHandler() *grpc.Server {
//conf := s.Config
grpclog.SetLoggerV2(zapgrpc.NewLogger(log.GetCallerSkipLogger(3).Logger))
grpclog.SetLoggerV2(zapgrpc.NewLogger(log.GetCallerSkipLogger(4).Logger))
if s.GrpcHandler != nil {
var stream = []grpc.StreamServerInterceptor{StreamAccess, StreamValidator}
var unary = []grpc.UnaryServerInterceptor{UnaryAccess(s.Config), UnaryValidator}
Expand Down
3 changes: 2 additions & 1 deletion server/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func newPropagator() propagation.TextMapPropagator {

func newTraceProvider(ctx context.Context) (*sdktrace.TracerProvider, error) {
traceExporter, err := stdouttrace.New(
stdouttrace.WithPrettyPrint())
//stdouttrace.WithPrettyPrint(),
)
if err != nil {
return nil, err
}
Expand Down
79 changes: 79 additions & 0 deletions utils/log/grpclog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package log

import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// grpclog
func (l *Logger) V(level int) bool {
level -= 2
return l.Logger.Core().Enabled(zapcore.Level(level))
}

// // 等同于xxxln,为了实现某些接口 如grpclog
func (l *Logger) Infoln(args ...any) {
if ce := l.Check(zap.InfoLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Warning(args ...any) {
if ce := l.Check(zap.WarnLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Warningln(args ...any) {
if ce := l.Check(zap.WarnLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

// grpclog
func (l *Logger) Warningf(template string, args ...any) {
if ce := l.Check(zap.WarnLevel, fmt.Sprintf(template, args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Errorln(args ...any) {
if ce := l.Check(zap.ErrorLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Fatalln(args ...any) {
if ce := l.Check(zap.FatalLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

// InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Println.
func (l *Logger) InfoDepth(depth int, args ...any) {
if ce := l.Check(zap.InfoLevel, trimLineBreak(fmt.Sprintln(args...))); ce != nil {
ce.Write()
}
}

// WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Println.
func (l *Logger) WarningDepth(depth int, args ...any) {
if ce := l.Check(zap.WarnLevel, trimLineBreak(fmt.Sprintln(args...))); ce != nil {
ce.Write()
}
}

// ErrorDepth logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Println.
func (l *Logger) ErrorDepth(depth int, args ...any) {
if ce := l.Check(zap.ErrorLevel, trimLineBreak(fmt.Sprintln(args...))); ce != nil {
ce.Write()
}
}

// FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Println.
func (l *Logger) FatalDepth(depth int, args ...any) {
if ce := l.Check(zap.FatalLevel, trimLineBreak(fmt.Sprintln(args...))); ce != nil {
ce.Write()
}
}
38 changes: 0 additions & 38 deletions utils/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ func (l *Logger) Warnf(template string, args ...any) {
}
}

// grpclog
func (l *Logger) Warningf(template string, args ...any) {
if ce := l.Check(zap.WarnLevel, fmt.Sprintf(template, args...)); ce != nil {
ce.Write()
}
}

// Errorf uses fmt.Sprintf to log a templated message.
func (l *Logger) Errorf(template string, args ...any) {
if ce := l.Check(zap.ErrorLevel, fmt.Sprintf(template, args...)); ce != nil {
Expand Down Expand Up @@ -242,37 +235,6 @@ func (l *Logger) Println(args ...any) {
}
}

// // 等同于xxxln,为了实现某些接口 如grpclog
func (l *Logger) Infoln(args ...any) {
if ce := l.Check(zap.InfoLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Warningln(args ...any) {
if ce := l.Check(zap.WarnLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Errorln(args ...any) {
if ce := l.Check(zap.ErrorLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

func (l *Logger) Fatalln(args ...any) {
if ce := l.Check(zap.FatalLevel, fmt.Sprint(args...)); ce != nil {
ce.Write()
}
}

// grpclog
func (l *Logger) V(level int) bool {
level -= 2
return l.Logger.Core().Enabled(zapcore.Level(level))
}

func (l *Logger) Debugfw(template string, args ...any) func(...zapcore.Field) {
return func(fields ...zapcore.Field) {
if ce := l.Check(zap.DebugLevel, fmt.Sprintf(template, args...)); ce != nil {
Expand Down
21 changes: 1 addition & 20 deletions utils/net/http/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ import (

var Internal = &metadata.MD{httpi.HeaderInternal: []string{"true"}}

var ClientConns = make(clientConns)

func ClientConnsClose() error {
if ClientConns != nil {
return ClientConns.Close()
}
return nil
}

type clientConns map[string]*grpc.ClientConn

func (cs clientConns) Close() error {
Expand All @@ -40,19 +31,13 @@ func (cs clientConns) Close() error {
}

func NewClient(addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
if conn, ok := ClientConns[addr]; ok {
return conn, nil
}

// Set up a connection to the server.
conn, err := grpc.NewClient(addr, append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(&stats.InternalClientHandler{}))...)
if err != nil {
return nil, err
}
if oldConn, ok := ClientConns[addr]; ok {
oldConn.Close()
}
ClientConns[addr] = conn

return conn, nil
}

Expand All @@ -62,9 +47,5 @@ func NewTLSClient(addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error
if err != nil {
return nil, err
}
if oldConn, ok := ClientConns[addr]; ok {
oldConn.Close()
}
ClientConns[addr] = conn
return conn, nil
}
2 changes: 1 addition & 1 deletion utils/net/http/grpc/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func init() {
grpclog.SetLoggerV2(zapgrpc.NewLogger(log.Default().Logger))
grpclog.SetLoggerV2(zapgrpc.NewLogger(log.GetCallerSkipLogger(4).Logger))
}

0 comments on commit 0e2e49d

Please sign in to comment.