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

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

@ -4,8 +4,6 @@ import (
"context"
"fmt"
"go.opentelemetry.io/otel/trace"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
@ -14,6 +12,8 @@ import (
// Server is an server logging 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(ctx context.Context, req interface{}) (reply interface{}, err error) {
reply, err = handler(ctx, req)
@ -32,6 +32,8 @@ func Server(logger log.Logger) middleware.Middleware {
// Client is an client logging 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(ctx context.Context, req interface{}) (reply interface{}, err error) {
reply, err = handler(ctx, req)
@ -55,17 +57,6 @@ func extractArgs(req interface{}) string {
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) {
if err != nil {
code = errors.Code(err)

Loading…
Cancel
Save