Skip to content

Commit

Permalink
Introducing option to disable log coloring
Browse files Browse the repository at this point in the history
NSM issue: networkservicemesh#1594

Signed-off-by: Botond Szirtes <botond.szirtes@est.tech>
  • Loading branch information
bszirtes committed Mar 18, 2024
1 parent b1a3e26 commit deb8e7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 7 additions & 7 deletions pkg/networkservice/core/trace/traceconcise/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func TestOutput(t *testing.T) {
require.NotNil(t, e)

expectedOutput :=
"\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-request={\"connection\":" +
" [INFO] [id:conn-1] [type:networkService] server-request={\"connection\":" +
"{\"id\":\"conn-1\",\"context\":{\"ip_context\":{\"src_ip_required\":true}}},\"mechanism_preferences\":" +
"[{\"cls\":\"LOCAL\",\"type\":\"KERNEL\"},{\"cls\":\"LOCAL\",\"type\":\"KERNEL\",\"parameters\":{\"label\":\"v2\"}}]}" +
"\n\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-request-response={\"id\":\"conn-1\",\"context\":" +
"\n [INFO] [id:conn-1] [type:networkService] server-request-response={\"id\":\"conn-1\",\"context\":" +
"{\"ip_context\":{\"src_ip_required\":true}},\"labels\":{\"Label\":\"B\"}}" +
"\n\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-close={\"id\":\"conn-1\",\"context\":{\"ip_context\":" +
"\n [INFO] [id:conn-1] [type:networkService] server-close={\"id\":\"conn-1\",\"context\":{\"ip_context\":" +
"{\"src_ip_required\":true}},\"labels\":{\"Label\":\"D\"}}" +
"\n\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-close-response={\"id\":\"conn-1\",\"context\":" +
"\n [INFO] [id:conn-1] [type:networkService] server-close-response={\"id\":\"conn-1\",\"context\":" +
"{\"ip_context\":{\"src_ip_required\":true}},\"labels\":{\"Label\":\"X\"}}\n"

result := testutil.TrimLogTime(&buff)
Expand Down Expand Up @@ -99,12 +99,12 @@ func TestErrorOutput(t *testing.T) {
require.Nil(t, conn)

expectedOutput :=
"\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-request={\"connection\":" +
" [INFO] [id:conn-1] [type:networkService] server-request={\"connection\":" +
"{\"id\":\"conn-1\",\"context\":{\"ip_context\":{\"src_ip_required\":true}}},\"mechanism_preferences\":" +
"[{\"cls\":\"LOCAL\",\"type\":\"KERNEL\"},{\"cls\":\"LOCAL\",\"type\":\"KERNEL\",\"parameters\":{\"label\":\"v2\"}}]}\n" +
"\x1b[36m [INFO] [id:conn-1] [type:networkService] \x1b[0mserver-request-response={\"id\":\"conn-1\",\"context\":" +
" [INFO] [id:conn-1] [type:networkService] server-request-response={\"id\":\"conn-1\",\"context\":" +
"{\"ip_context\":{\"src_ip_required\":true}},\"labels\":{\"Label\":\"B\"}}\n" +
"\x1b[31m [ERRO] [id:conn-1] [type:networkService] \x1b[0mError returned from sdk/pkg/networkservice/core/trace/testutil/ErrorServer.Request:" +
" [ERRO] [id:conn-1] [type:networkService] Error returned from sdk/pkg/networkservice/core/trace/testutil/ErrorServer.Request:" +
" Error returned from api/pkg/api/networkservice/networkServiceClient.Close;" +
"\tgithub.com/networkservicemesh/sdk/pkg/networkservice/core/trace.(*beginTraceClient).Close;" +
"\t\t/root/go/pkg/mod/github.com/networkservicemesh/sdk@v0.5.1-0.20210929180427-ec235de055f1/pkg/networkservice/core/trace/client.go:85;" +
Expand Down
10 changes: 4 additions & 6 deletions pkg/tools/log/logruslogger/formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2024 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -33,6 +33,7 @@ type formatter struct {
func newFormatter() *formatter {
f := formatter{}
f.nf.FieldsOrder = []string{"id", "name"}
f.nf.NoColors = true
return &f
}

Expand All @@ -52,11 +53,8 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
// output buffer
bb := &bytes.Buffer{}

split := strings.SplitN(bytesString, "\x1b[0m", 2)
prefix := split[0] + "\x1b[0m"
split[1] = split[1][:len(split[1])-1] // remove trailing \n
bb.WriteString(prefix)
for _, line := range strings.Split(split[1], "\n") {
bytesString = bytesString[:len(bytesString)-1] // remove trailing \n
for _, line := range strings.Split(bytesString, "\n") {
bb.WriteString(line)
bb.WriteString(";\t")
}
Expand Down

0 comments on commit deb8e7d

Please sign in to comment.