add request params

pull/715/head
chenzhihui 4 years ago
parent e25ebc73bc
commit 1ee7e70939
  1. 24
      middleware/logging/logging.go

@ -2,6 +2,7 @@ package logging
import ( import (
"context" "context"
"fmt"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log" "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(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) { return func(ctx context.Context, req interface{}) (interface{}, error) {
var ( var (
path string path string
method string method string
params string
component string
) )
if info, ok := http.FromServerContext(ctx); ok { if info, ok := http.FromServerContext(ctx); ok {
component = "HTTP"
path = info.Request.RequestURI path = info.Request.RequestURI
method = info.Request.Method method = info.Request.Method
params = info.Request.Form.Encode()
} else if info, ok := grpc.FromServerContext(ctx); ok { } else if info, ok := grpc.FromServerContext(ctx); ok {
component = "gRPC"
path = info.FullMethod path = info.FullMethod
method = "POST" method = "POST"
params = req.(fmt.Stringer).String()
} }
reply, err := handler(ctx, req) reply, err := handler(ctx, req)
if err != nil { if err != nil {
log.Errorw( log.Errorw(
"kind", "server", "kind", "server",
"component", component,
"path", path, "path", path,
"method", method, "method", method,
"params", params,
"code", errors.Code(err), "code", errors.Code(err),
"error", err.Error(), "error", err.Error(),
) )
@ -59,8 +68,10 @@ func Server(opts ...Option) middleware.Middleware {
} }
log.Infow( log.Infow(
"kind", "server", "kind", "server",
"component", component,
"path", path, "path", path,
"method", method, "method", method,
"params", params,
"code", 0, "code", 0,
) )
return reply, nil return reply, nil
@ -80,18 +91,21 @@ func Client(opts ...Option) middleware.Middleware {
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) { return func(ctx context.Context, req interface{}) (interface{}, error) {
var ( var (
component string
path string path string
method string method string
params string
component string
) )
if info, ok := http.FromClientContext(ctx); ok { if info, ok := http.FromClientContext(ctx); ok {
component = "HTTP" component = "HTTP"
path = info.Request.RequestURI path = info.Request.RequestURI
method = info.Request.Method method = info.Request.Method
params = info.Request.Form.Encode()
} else if info, ok := grpc.FromClientContext(ctx); ok { } else if info, ok := grpc.FromClientContext(ctx); ok {
component = "gRPC"
path = info.FullMethod path = info.FullMethod
method = "POST" method = "POST"
component = "gRPC"
params = req.(fmt.Stringer).String()
} }
reply, err := handler(ctx, req) reply, err := handler(ctx, req)
if err != nil { if err != nil {
@ -100,6 +114,7 @@ func Client(opts ...Option) middleware.Middleware {
"component", component, "component", component,
"path", path, "path", path,
"method", method, "method", method,
"params", params,
"code", errors.Code(err), "code", errors.Code(err),
"error", err.Error(), "error", err.Error(),
) )
@ -110,6 +125,7 @@ func Client(opts ...Option) middleware.Middleware {
"component", component, "component", component,
"path", path, "path", path,
"method", method, "method", method,
"params", params,
"code", 0, "code", 0,
) )
return reply, nil return reply, nil

Loading…
Cancel
Save