fix loggin middleware

pull/1001/head
longXboy 4 years ago committed by longxboy
parent fd84ed4b52
commit bf00542cb0
  1. 10
      middleware/logging/grpc.go
  2. 10
      middleware/logging/http.go
  3. 17
      middleware/logging/logging.go

@ -12,17 +12,14 @@ func grpcServerLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
traceID, spanID := extractTrace(ctx)
code, errMsg := extractError(err) code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
logger.Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "server", "kind", "server",
"component", "grpc", "component", "grpc",
"trace_id", traceID,
"span_id", spanID,
"grpc.target", info.FullMethod, "grpc.target", info.FullMethod,
"grpc.args", args, "grpc.args", args,
"grpc.code", code, "grpc.code", code,
@ -35,17 +32,14 @@ func grpcClientLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
traceID, spanID := extractTrace(ctx)
code, errMsg := extractError(err) code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
logger.Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "client", "kind", "client",
"component", "grpc", "component", "grpc",
"trace_id", traceID,
"span_id", spanID,
"grpc.target", info.FullMethod, "grpc.target", info.FullMethod,
"grpc.args", args, "grpc.args", args,
"grpc.code", code, "grpc.code", code,

@ -12,17 +12,14 @@ func httpServerLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
traceID, spanID := extractTrace(ctx)
code, errMsg := extractError(err) code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
logger.Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "server", "kind", "server",
"component", "http", "component", "http",
"trace_id", traceID,
"span_id", spanID,
"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,
@ -36,17 +33,14 @@ func httpClientLog(logger log.Logger, ctx context.Context, args string, err erro
if !ok { if !ok {
return return
} }
traceID, spanID := extractTrace(ctx)
code, errMsg := extractError(err) code, errMsg := extractError(err)
level := log.LevelInfo level := log.LevelInfo
if err != nil { if err != nil {
level = log.LevelError level = log.LevelError
} }
logger.Log(level, log.WithContext(ctx, logger).Log(level,
"kind", "client", "kind", "client",
"component", "http", "component", "http",
"trace_id", traceID,
"span_id", spanID,
"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,

@ -4,8 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"go.opentelemetry.io/otel/trace"
"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"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
@ -14,6 +12,8 @@ import (
// Server is an server logging middleware. // Server is an server logging middleware.
func Server(logger log.Logger) middleware.Middleware { func Server(logger log.Logger) middleware.Middleware {
logger = log.With(logger, "trace_id", log.TraceID(), "span_id", log.SpanID())
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
reply, err = handler(ctx, req) reply, err = handler(ctx, req)
@ -32,6 +32,8 @@ func Server(logger log.Logger) middleware.Middleware {
// Client is an client logging middleware. // Client is an client logging middleware.
func Client(logger log.Logger) middleware.Middleware { func Client(logger log.Logger) middleware.Middleware {
logger = log.With(logger, "trace_id", log.TraceID(), "span_id", log.SpanID())
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
reply, err = handler(ctx, req) reply, err = handler(ctx, req)
@ -55,17 +57,6 @@ func extractArgs(req interface{}) string {
return fmt.Sprintf("%+v", req) return fmt.Sprintf("%+v", req)
} }
func extractTrace(ctx context.Context) (traceID, spanID string) {
span := trace.SpanContextFromContext(ctx)
if span.HasTraceID() {
traceID = span.TraceID().String()
}
if span.HasSpanID() {
spanID = span.SpanID().String()
}
return
}
func extractError(err error) (code int, errMsg string) { func extractError(err error) (code int, errMsg string) {
if err != nil { if err != nil {
code = errors.Code(err) code = errors.Code(err)

Loading…
Cancel
Save