diff --git a/errors/errors.go b/errors/errors.go index b80cde056..2f76d10d8 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -76,7 +76,7 @@ func Errorf(code int, reason, format string, a ...interface{}) error { // It supports wrapped errors. func Code(err error) int { if err == nil { - return 0 + return 200 } if se := FromError(err); err != nil { return int(se.Code) diff --git a/middleware/logging/grpc.go b/middleware/logging/grpc.go index e4057ae7a..3fa46f5fa 100644 --- a/middleware/logging/grpc.go +++ b/middleware/logging/grpc.go @@ -3,6 +3,8 @@ package logging import ( "context" + "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/internal/httputil" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/transport/grpc" ) @@ -12,18 +14,19 @@ func grpcServerLog(logger log.Logger, ctx context.Context, args string, err erro if !ok { return } - code, errMsg := extractError(err) + level := log.LevelInfo if err != nil { level = log.LevelError } + log.WithContext(ctx, logger).Log(level, "kind", "server", "component", "grpc", "grpc.target", info.FullMethod, "grpc.args", args, - "grpc.code", code, - "grpc.error", errMsg, + "grpc.code", httputil.GRPCCodeFromStatus(errors.Code(err)), + "grpc.error", extractError(err), ) } @@ -32,7 +35,6 @@ func grpcClientLog(logger log.Logger, ctx context.Context, args string, err erro if !ok { return } - code, errMsg := extractError(err) level := log.LevelInfo if err != nil { level = log.LevelError @@ -42,7 +44,7 @@ func grpcClientLog(logger log.Logger, ctx context.Context, args string, err erro "component", "grpc", "grpc.target", info.FullMethod, "grpc.args", args, - "grpc.code", code, - "grpc.error", errMsg, + "grpc.code", httputil.GRPCCodeFromStatus(errors.Code(err)), + "grpc.error", extractError(err), ) } diff --git a/middleware/logging/http.go b/middleware/logging/http.go index 3514e73d7..31a3c6557 100644 --- a/middleware/logging/http.go +++ b/middleware/logging/http.go @@ -3,6 +3,7 @@ package logging import ( "context" + "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/transport/http" ) @@ -12,19 +13,20 @@ func httpServerLog(logger log.Logger, ctx context.Context, args string, err erro if !ok { return } - code, errMsg := extractError(err) + level := log.LevelInfo if err != nil { level = log.LevelError } + log.WithContext(ctx, logger).Log(level, "kind", "server", "component", "http", "http.target", info.Request.RequestURI, "http.method", info.Request.Method, "http.args", args, - "http.code", code, - "http.error", errMsg, + "http.code", errors.Code(err), + "http.error", extractError(err), ) } @@ -33,18 +35,19 @@ func httpClientLog(logger log.Logger, ctx context.Context, args string, err erro if !ok { return } - code, errMsg := extractError(err) + level := log.LevelInfo if err != nil { level = log.LevelError } + log.WithContext(ctx, logger).Log(level, "kind", "client", "component", "http", "http.target", info.Request.RequestURI, "http.method", info.Request.Method, "http.args", args, - "http.code", code, - "http.error", errMsg, + "http.code", errors.Code(err), + "http.error", extractError(err), ) } diff --git a/middleware/logging/logging.go b/middleware/logging/logging.go index 83cb99c9a..e39aab698 100644 --- a/middleware/logging/logging.go +++ b/middleware/logging/logging.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/transport" @@ -53,9 +52,8 @@ func extractArgs(req interface{}) string { return fmt.Sprintf("%+v", req) } -func extractError(err error) (code int, errMsg string) { +func extractError(err error) (errMsg string) { if err != nil { - code = errors.Code(err) errMsg = fmt.Sprintf("%+v", err) } return diff --git a/middleware/logging/logging_test.go b/middleware/logging/logging_test.go new file mode 100644 index 000000000..66b266ee2 --- /dev/null +++ b/middleware/logging/logging_test.go @@ -0,0 +1,76 @@ +package logging + +import ( + "bytes" + "context" + "errors" + "net/http/httptest" + "testing" + + "github.com/go-kratos/kratos/v2/log" + "github.com/go-kratos/kratos/v2/middleware" + "github.com/go-kratos/kratos/v2/transport" + "github.com/go-kratos/kratos/v2/transport/http" +) + +func TestHTTP(t *testing.T) { + var req = httptest.NewRequest("GET", "http://example.com/foo", nil) + var err = errors.New("reply.error") + var bf = bytes.NewBuffer(nil) + var logger = log.NewStdLogger(bf) + + tests := []struct { + name string + kind func(logger log.Logger) middleware.Middleware + err error + ctx context.Context + }{ + {"http-server@fail", + Server, + err, + func() context.Context { + res := httptest.NewRecorder() + ctx := transport.NewContext(context.Background(), transport.Transport{Kind: transport.KindHTTP, Endpoint: "endpoint"}) + return http.NewServerContext(ctx, http.ServerInfo{Request: req, Response: res}) + }(), + }, + {"http-server@succ", + Server, + nil, + func() context.Context { + res := httptest.NewRecorder() + ctx := transport.NewContext(context.Background(), transport.Transport{Kind: transport.KindHTTP, Endpoint: "endpoint"}) + return http.NewServerContext(ctx, http.ServerInfo{Request: req, Response: res}) + }(), + }, + {"http-client@succ", + Client, + nil, + func() context.Context { + ctx := transport.NewContext(context.Background(), transport.Transport{Kind: transport.KindHTTP, Endpoint: "endpoint"}) + return http.NewClientContext(ctx, http.ClientInfo{Request: req, PathPattern: "{name}"}) + }(), + }, + {"http-client@fail", + Client, + err, + func() context.Context { + ctx := transport.NewContext(context.Background(), transport.Transport{Kind: transport.KindHTTP, Endpoint: "endpoint"}) + return http.NewClientContext(ctx, http.ClientInfo{Request: req, PathPattern: "{name}"}) + }(), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + bf.Reset() + next := func(ctx context.Context, req interface{}) (interface{}, error) { + return "reply", test.err + } + next = test.kind(logger)(next) + v, e := next(test.ctx, "req.args") + t.Logf("[%s]reply: %v, error: %v", test.name, v, e) + t.Logf("[%s]buffer:%s", test.name, bf.String()) + }) + } +}