From 1ee7e7093932aac71064192b4435f42f663f29a0 Mon Sep 17 00:00:00 2001 From: chenzhihui Date: Sun, 21 Feb 2021 17:35:10 +0800 Subject: [PATCH] add request params --- middleware/logging/logging.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/middleware/logging/logging.go b/middleware/logging/logging.go index 2486b0603..7983d1e94 100644 --- a/middleware/logging/logging.go +++ b/middleware/logging/logging.go @@ -2,6 +2,7 @@ package logging import ( "context" + "fmt" "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/log" @@ -36,22 +37,30 @@ func Server(opts ...Option) middleware.Middleware { return func(handler middleware.Handler) middleware.Handler { return func(ctx context.Context, req interface{}) (interface{}, error) { var ( - path string - method string + path string + method string + params string + component string ) if info, ok := http.FromServerContext(ctx); ok { + component = "HTTP" path = info.Request.RequestURI method = info.Request.Method + params = info.Request.Form.Encode() } else if info, ok := grpc.FromServerContext(ctx); ok { + component = "gRPC" path = info.FullMethod method = "POST" + params = req.(fmt.Stringer).String() } reply, err := handler(ctx, req) if err != nil { log.Errorw( "kind", "server", + "component", component, "path", path, "method", method, + "params", params, "code", errors.Code(err), "error", err.Error(), ) @@ -59,8 +68,10 @@ func Server(opts ...Option) middleware.Middleware { } log.Infow( "kind", "server", + "component", component, "path", path, "method", method, + "params", params, "code", 0, ) return reply, nil @@ -80,18 +91,21 @@ func Client(opts ...Option) middleware.Middleware { return func(handler middleware.Handler) middleware.Handler { return func(ctx context.Context, req interface{}) (interface{}, error) { var ( - component string path string method string + params string + component string ) if info, ok := http.FromClientContext(ctx); ok { component = "HTTP" path = info.Request.RequestURI method = info.Request.Method + params = info.Request.Form.Encode() } else if info, ok := grpc.FromClientContext(ctx); ok { - component = "gRPC" path = info.FullMethod method = "POST" + component = "gRPC" + params = req.(fmt.Stringer).String() } reply, err := handler(ctx, req) if err != nil { @@ -100,6 +114,7 @@ func Client(opts ...Option) middleware.Middleware { "component", component, "path", path, "method", method, + "params", params, "code", errors.Code(err), "error", err.Error(), ) @@ -110,6 +125,7 @@ func Client(opts ...Option) middleware.Middleware { "component", component, "path", path, "method", method, + "params", params, "code", 0, ) return reply, nil