From bae0101b91dd1b70b2e782e95f28fba046263efd Mon Sep 17 00:00:00 2001 From: drmac Date: Wed, 21 Apr 2021 16:19:42 +0000 Subject: [PATCH] support to remove colors when writing to text files --- hooks/writer/writer.go | 13 +++++++++++- text_formatter.go | 45 ++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/hooks/writer/writer.go b/hooks/writer/writer.go index 1160c79..ce369b2 100644 --- a/hooks/writer/writer.go +++ b/hooks/writer/writer.go @@ -10,15 +10,26 @@ import ( type Hook struct { Writer io.Writer LogLevels []log.Level + Formatter log.Formatter } // Fire will be called when some logging function is called with current hook // It will format log entry to string and write it to appropriate writer func (hook *Hook) Fire(entry *log.Entry) error { - line, err := entry.Bytes() + + var line []byte + var err error + + if hook.Formatter != nil { + line, err = hook.Formatter.Format(entry) + } else { + line, err = entry.Bytes() + } + if err != nil { return err } + _, err = hook.Writer.Write(line) return err } diff --git a/text_formatter.go b/text_formatter.go index be2c6ef..21d4022 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -199,37 +199,14 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { if timestampFormat == "" { timestampFormat = defaultTimestampFormat } - if f.isColored() { - f.printColored(b, entry, keys, data, timestampFormat) - } else { - for _, key := range fixedKeys { - var value interface{} - switch { - case key == f.FieldMap.resolve(FieldKeyTime): - value = entry.Time.Format(timestampFormat) - case key == f.FieldMap.resolve(FieldKeyLevel): - value = entry.Level.String() - case key == f.FieldMap.resolve(FieldKeyMsg): - value = entry.Message - case key == f.FieldMap.resolve(FieldKeyLogrusError): - value = entry.err - case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller(): - value = funcVal - case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller(): - value = fileVal - default: - value = data[key] - } - f.appendKeyValue(b, key, value) - } - } + f.printer(b, entry, keys, data, timestampFormat) b.WriteByte('\n') return b.Bytes(), nil } -func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) { +func (f *TextFormatter) printer(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) { var levelColor int switch entry.Level { case DebugLevel, TraceLevel: @@ -282,15 +259,27 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin switch { case f.DisableTimestamp: - fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message) + if f.DisableColors { + fmt.Fprintf(b, "%s%s %-44s ", levelText, caller, entry.Message) + } else { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message) + } case !f.FullTimestamp: fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message) default: - fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message) + if f.DisableColors { + fmt.Fprintf(b, "%s[%s]%s %-44s ", levelText, entry.Time.Format(timestampFormat), caller, entry.Message) + } else { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message) + } } for _, k := range keys { v := data[k] - fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k) + if f.DisableColors { + fmt.Fprintf(b, " %s=", k) + } else { + fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k) + } f.appendValue(b, v) } }