Skip to content

Commit

Permalink
use the logrus logger by default to not miss useful logs.
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin committed Dec 19, 2024
1 parent 7dfb136 commit 9586bdd
Showing 5 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/networkservice/core/trace/client.go
Original file line number Diff line number Diff line change
@@ -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 {
8 changes: 8 additions & 0 deletions pkg/networkservice/core/trace/server.go
Original file line number Diff line number Diff line change
@@ -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 {
33 changes: 33 additions & 0 deletions pkg/networkservice/core/trace/server_test.go
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions pkg/registry/core/trace/ns_registry.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions pkg/registry/core/trace/nse_registry.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9586bdd

Please sign in to comment.