Browse Source

调整logger

dcsunny 2 năm trước cách đây
mục cha
commit
f76252d17d
2 tập tin đã thay đổi với 11 bổ sung4 xóa
  1. 3 1
      grpc/client.go
  2. 8 3
      http/middleware/logging/logging.go

+ 3 - 1
grpc/client.go

@@ -32,7 +32,9 @@ func GetDialInsecure(
 		return connMap[clientName], nil
 	}
 	endpoint := grpc.WithEndpoint(endpointNameKey)
-	option.Middlewares = append(option.Middlewares, logging.Client(logger))
+	if logger != nil {
+		option.Middlewares = append(option.Middlewares, logging.Client(logger))
+	}
 	if r == nil {
 		return nil, nil
 	}

+ 8 - 3
http/middleware/logging/logging.go

@@ -2,9 +2,11 @@ package logging
 
 import (
 	"context"
+	"encoding/json"
 	"fmt"
 	"time"
 
+	"git.ikuban.com/server/kratos-utils/codes"
 	"github.com/go-kratos/kratos/v2/errors"
 	"github.com/go-kratos/kratos/v2/log"
 	"github.com/go-kratos/kratos/v2/middleware"
@@ -75,7 +77,6 @@ func Client(logger log.Logger) middleware.Middleware {
 			}
 			level, stack := extractError(err)
 			log.WithContext(ctx, logger).Log(level,
-				"kind", "client",
 				"component", kind,
 				"operation", operation,
 				"args", extractArgs(req),
@@ -94,13 +95,17 @@ func extractArgs(req interface{}) string {
 	if stringer, ok := req.(fmt.Stringer); ok {
 		return stringer.String()
 	}
-	return fmt.Sprintf("%+v", req)
+	j, err := json.Marshal(req)
+	if err != nil {
+		return fmt.Sprintf("%+v", req)
+	}
+	return string(j)
 }
 
 // extractError returns the string of the error
 func extractError(err error) (log.Level, string) {
 	if err != nil {
-		return log.LevelError, fmt.Sprintf("%+v", err)
+		return log.LevelError, fmt.Sprintf("%+v", codes.ParseErrorMsg(err))
 	}
 	return log.LevelInfo, ""
 }