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 14, 2024
1 parent b1a3e26 commit 65b0cd1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/tools/log/logruslogger/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package logruslogger

import (
"bytes"
"os"
"strings"

nested "github.com/antonfisher/nested-logrus-formatter"
Expand All @@ -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
}

Expand All @@ -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")
}
Expand Down

0 comments on commit 65b0cd1

Please sign in to comment.