From 65b0cd1eb8dc14b65ce8e8501740c76645c303a3 Mon Sep 17 00:00:00 2001 From: Botond Szirtes Date: Wed, 13 Mar 2024 16:28:11 +0100 Subject: [PATCH] Introducing option to disable log coloring NSM issue: https://github.com/networkservicemesh/sdk/issues/1594 Signed-off-by: Botond Szirtes --- pkg/tools/log/logruslogger/formatter.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/tools/log/logruslogger/formatter.go b/pkg/tools/log/logruslogger/formatter.go index 5e7d8bde2..930d322e2 100644 --- a/pkg/tools/log/logruslogger/formatter.go +++ b/pkg/tools/log/logruslogger/formatter.go @@ -18,6 +18,7 @@ package logruslogger import ( "bytes" + "os" "strings" nested "github.com/antonfisher/nested-logrus-formatter" @@ -33,6 +34,9 @@ type formatter struct { func newFormatter() *formatter { f := formatter{} f.nf.FieldsOrder = []string{"id", "name"} + if os.Getenv("LOG_NOCOLORS") == "true" { + f.nf.NoColors = true + } return &f } @@ -53,10 +57,16 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { 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 + + prefix := "" + if !f.nf.NoColors { + prefix = split[0] + "\x1b[0m" + split = split[1:] + } + + split[0] = split[0][:len(split[0])-1] // remove trailing \n bb.WriteString(prefix) - for _, line := range strings.Split(split[1], "\n") { + for _, line := range strings.Split(split[0], "\n") { bb.WriteString(line) bb.WriteString(";\t") }