From 7c581401326f089f3a5800441e150b333b882aab Mon Sep 17 00:00:00 2001 From: Tim Liu Date: Sat, 29 Aug 2020 08:39:11 +0000 Subject: [PATCH] Add option NoFieldsSpace --- README.md | 3 +++ formatter.go | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a5c91d2..befb071 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ type Formatter struct { // NoFieldsColors - apply colors only to the level, default is level + fields NoFieldsColors bool + // NoFieldsSpace - no space between fields + NoFieldsSpace bool + // ShowFullLevel - show a full level [WARNING] instead of [WARN] ShowFullLevel bool diff --git a/formatter.go b/formatter.go index cbf6db1..2c5daf7 100644 --- a/formatter.go +++ b/formatter.go @@ -28,6 +28,9 @@ type Formatter struct { // NoFieldsColors - apply colors only to the level, default is level + fields NoFieldsColors bool + // NoFieldsSpace - no space between fields + NoFieldsSpace bool + // ShowFullLevel - show a full level [WARNING] instead of [WARN] ShowFullLevel bool @@ -73,7 +76,11 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { } else { b.WriteString(level[:4]) } - b.WriteString("] ") + b.WriteString("]") + + if !f.NoFieldsSpace { + b.WriteString(" ") + } if !f.NoColors && f.NoFieldsColors { b.WriteString("\x1b[0m") @@ -86,6 +93,10 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { f.writeOrderedFields(b, entry) } + if f.NoFieldsSpace { + b.WriteString(" ") + } + if !f.NoColors && !f.NoFieldsColors { b.WriteString("\x1b[0m") } @@ -166,9 +177,13 @@ func (f *Formatter) writeOrderedFields(b *bytes.Buffer, entry *logrus.Entry) { func (f *Formatter) writeField(b *bytes.Buffer, entry *logrus.Entry, field string) { if f.HideKeys { - fmt.Fprintf(b, "[%v] ", entry.Data[field]) + fmt.Fprintf(b, "[%v]", entry.Data[field]) } else { - fmt.Fprintf(b, "[%s:%v] ", field, entry.Data[field]) + fmt.Fprintf(b, "[%s:%v]", field, entry.Data[field]) + } + + if !f.NoFieldsSpace { + b.WriteString(" ") } }