diff --git a/pkg/networkservice/core/trace/client.go b/pkg/networkservice/core/trace/client.go index 93c7b3f3c..cc6cf4882 100644 --- a/pkg/networkservice/core/trace/client.go +++ b/pkg/networkservice/core/trace/client.go @@ -29,6 +29,8 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceconcise" "github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceverbose" + "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" ) type traceClient struct { @@ -48,16 +50,23 @@ func NewNetworkServiceClient(traced networkservice.NetworkServiceClient) network func (t *traceClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { if logrus.GetLevel() <= logrus.WarnLevel { + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Request(ctx, request) } if logrus.GetLevel() >= logrus.DebugLevel { return t.verbose.Request(ctx, request, opts...) } + return t.concise.Request(ctx, request, opts...) } func (t *traceClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { if logrus.GetLevel() <= logrus.WarnLevel { + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Close(ctx, conn) } if logrus.GetLevel() >= logrus.DebugLevel { diff --git a/pkg/networkservice/core/trace/server.go b/pkg/networkservice/core/trace/server.go index dc3d0e635..4f702aa80 100644 --- a/pkg/networkservice/core/trace/server.go +++ b/pkg/networkservice/core/trace/server.go @@ -23,6 +23,8 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceconcise" "github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceverbose" + "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" "github.com/golang/protobuf/ptypes/empty" @@ -44,6 +46,9 @@ func NewNetworkServiceServer(traced networkservice.NetworkServiceServer) network func (t *traceServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { if logrus.GetLevel() <= logrus.WarnLevel { + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Request(ctx, request) } if logrus.GetLevel() >= logrus.DebugLevel { @@ -55,6 +60,9 @@ func (t *traceServer) Request(ctx context.Context, request *networkservice.Netwo func (t *traceServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { if logrus.GetLevel() <= logrus.WarnLevel { + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Close(ctx, conn) } if logrus.GetLevel() >= logrus.DebugLevel { diff --git a/pkg/networkservice/core/trace/server_test.go b/pkg/networkservice/core/trace/server_test.go index 71cbb12d3..b631cd1a8 100644 --- a/pkg/networkservice/core/trace/server_test.go +++ b/pkg/networkservice/core/trace/server_test.go @@ -33,6 +33,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" "github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/testutil" + "github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkcontext" "github.com/networkservicemesh/sdk/pkg/tools/log" ) @@ -165,6 +166,38 @@ func TestErrorOutput(t *testing.T) { require.Equal(t, expectedOutput, result) } +func Test_ErrorOutput_InfoLevel(t *testing.T) { + // Configure logging + // Set output to buffer + var buff bytes.Buffer + logrus.SetOutput(&buff) + logrus.SetFormatter(&logrus.TextFormatter{ + DisableTimestamp: true, + }) + logrus.SetLevel(logrus.WarnLevel) + log.EnableTracing(true) + + // Create a chain with modifying elements + ch := chain.NewNetworkServiceServer( + checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) { + log.FromContext(ctx).Error("error details") + }), + ) + + request := testutil.NewConnection() + + _, err := ch.Request(context.Background(), request) + require.NoError(t, err) + + expectedOutput := "[ERRO] error details\n" + result := testutil.TrimLogTime(&buff) + + result = testutil.Normalize(result) + expectedOutput = testutil.Normalize(expectedOutput) + + require.Equal(t, expectedOutput, result) +} + func TestOutput_Info(t *testing.T) { // Configure logging // Set output to buffer diff --git a/pkg/registry/core/trace/ns_registry.go b/pkg/registry/core/trace/ns_registry.go index f6256b2cb..a4934319a 100644 --- a/pkg/registry/core/trace/ns_registry.go +++ b/pkg/registry/core/trace/ns_registry.go @@ -28,6 +28,8 @@ import ( "github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceconcise" "github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceverbose" + "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" ) type traceNetworkServiceRegistryClient struct { @@ -50,6 +52,9 @@ func (t *traceNetworkServiceRegistryClient) Register(ctx context.Context, in *re if logrus.GetLevel() >= logrus.DebugLevel { return t.verbose.Register(ctx, in, opts...) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Register(ctx, in, opts...) } func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) { @@ -59,6 +64,9 @@ func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *regist if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Find(ctx, in, opts...) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Find(ctx, in, opts...) } @@ -69,6 +77,9 @@ func (t *traceNetworkServiceRegistryClient) Unregister(ctx context.Context, in * if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Unregister(ctx, in, opts...) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Unregister(ctx, in, opts...) } @@ -92,6 +103,9 @@ func (t *traceNetworkServiceRegistryServer) Register(ctx context.Context, in *re if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Register(ctx, in) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Register(ctx, in) } @@ -112,5 +126,8 @@ func (t *traceNetworkServiceRegistryServer) Unregister(ctx context.Context, in * if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Unregister(ctx, in) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Unregister(ctx, in) } diff --git a/pkg/registry/core/trace/nse_registry.go b/pkg/registry/core/trace/nse_registry.go index 52564eba3..b4f14ab46 100644 --- a/pkg/registry/core/trace/nse_registry.go +++ b/pkg/registry/core/trace/nse_registry.go @@ -27,6 +27,8 @@ import ( "github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceconcise" "github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceverbose" + "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" ) type traceNetworkServiceEndpointRegistryClient struct { @@ -59,6 +61,9 @@ func (t *traceNetworkServiceEndpointRegistryClient) Find(ctx context.Context, in if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Find(ctx, in, opts...) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Find(ctx, in, opts...) } @@ -69,6 +74,9 @@ func (t *traceNetworkServiceEndpointRegistryClient) Unregister(ctx context.Conte if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Unregister(ctx, in, opts...) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Unregister(ctx, in, opts...) } @@ -92,6 +100,9 @@ func (t *traceNetworkServiceEndpointRegistryServer) Register(ctx context.Context if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Register(ctx, in) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Register(ctx, in) } @@ -112,5 +123,8 @@ func (t *traceNetworkServiceEndpointRegistryServer) Unregister(ctx context.Conte if logrus.GetLevel() <= logrus.WarnLevel { return t.concise.Unregister(ctx, in) } + if log.FromContext(ctx) == log.L() { + ctx = log.WithLog(ctx, logruslogger.New(ctx)) + } return t.original.Unregister(ctx, in) }