mw/log: http code 0 (#1010)

* fix middleware/log default code
pull/1017/head
miya 3 years ago committed by GitHub
parent 5cbda8ebd7
commit 544e08f729
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      errors/errors.go
  2. 14
      middleware/logging/grpc.go
  3. 15
      middleware/logging/http.go
  4. 4
      middleware/logging/logging.go
  5. 76
      middleware/logging/logging_test.go

@ -76,7 +76,7 @@ func Errorf(code int, reason, format string, a ...interface{}) error {
// It supports wrapped errors. // It supports wrapped errors.
func Code(err error) int { func Code(err error) int {
if err == nil { if err == nil {
return 0 return 200
} }
if se := FromError(err); err != nil { if se := FromError(err); err != nil {
return int(se.Code) return int(se.Code)

@ -3,6 +3,8 @@ package logging
import ( import (
"context" "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/log"
"github.com/go-kratos/kratos/v2/transport/grpc" "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 { if !ok {
return return
} }
code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
log.WithContext(ctx, logger).Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "server", "kind", "server",
"component", "grpc", "component", "grpc",
"grpc.target", info.FullMethod, "grpc.target", info.FullMethod,
"grpc.args", args, "grpc.args", args,
"grpc.code", code, "grpc.code", httputil.GRPCCodeFromStatus(errors.Code(err)),
"grpc.error", errMsg, "grpc.error", extractError(err),
) )
} }
@ -32,7 +35,6 @@ func grpcClientLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
@ -42,7 +44,7 @@ func grpcClientLog(logger log.Logger, ctx context.Context, args string, err erro
"component", "grpc", "component", "grpc",
"grpc.target", info.FullMethod, "grpc.target", info.FullMethod,
"grpc.args", args, "grpc.args", args,
"grpc.code", code, "grpc.code", httputil.GRPCCodeFromStatus(errors.Code(err)),
"grpc.error", errMsg, "grpc.error", extractError(err),
) )
} }

@ -3,6 +3,7 @@ package logging
import ( import (
"context" "context"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport/http" "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 { if !ok {
return return
} }
code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
log.WithContext(ctx, logger).Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "server", "kind", "server",
"component", "http", "component", "http",
"http.target", info.Request.RequestURI, "http.target", info.Request.RequestURI,
"http.method", info.Request.Method, "http.method", info.Request.Method,
"http.args", args, "http.args", args,
"http.code", code, "http.code", errors.Code(err),
"http.error", errMsg, "http.error", extractError(err),
) )
} }
@ -33,18 +35,19 @@ func httpClientLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
log.WithContext(ctx, logger).Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "client", "kind", "client",
"component", "http", "component", "http",
"http.target", info.Request.RequestURI, "http.target", info.Request.RequestURI,
"http.method", info.Request.Method, "http.method", info.Request.Method,
"http.args", args, "http.args", args,
"http.code", code, "http.code", errors.Code(err),
"http.error", errMsg, "http.error", extractError(err),
) )
} }

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
@ -53,9 +52,8 @@ func extractArgs(req interface{}) string {
return fmt.Sprintf("%+v", req) return fmt.Sprintf("%+v", req)
} }
func extractError(err error) (code int, errMsg string) { func extractError(err error) (errMsg string) {
if err != nil { if err != nil {
code = errors.Code(err)
errMsg = fmt.Sprintf("%+v", err) errMsg = fmt.Sprintf("%+v", err)
} }
return return

@ -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())
})
}
}
Loading…
Cancel
Save