From b0016339806f3a6f25869c33cee70329cbeac4bd Mon Sep 17 00:00:00 2001
From: tim-ywliu <68043973+tim-ywliu@users.noreply.github.com>
Date: Thu, 3 Sep 2020 12:08:02 +0800
Subject: [PATCH] feat: add NoFieldsSpace option (#5)

* Add option NoFieldsSpace

* tests: add tests for NoFieldsSpace option
---
 README.md               |  3 +++
 formatter.go            | 21 ++++++++++++++++++---
 tests/formatter_test.go | 25 +++++++++++++++++++++++++
 3 files changed, 46 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(" ")
 	}
 }
 
diff --git a/tests/formatter_test.go b/tests/formatter_test.go
index c0fe259..dcc85d0 100644
--- a/tests/formatter_test.go
+++ b/tests/formatter_test.go
@@ -143,6 +143,31 @@ func ExampleFormatter_Format_field_order() {
 	// - [INFO] [component:main] [category:rest] test3
 }
 
+func ExampleFormatter_Format_no_fields_space() {
+	l := logrus.New()
+	l.SetOutput(os.Stdout)
+	l.SetLevel(logrus.DebugLevel)
+	l.SetFormatter(&formatter.Formatter{
+		NoColors:        true,
+		TimestampFormat: "-",
+		FieldsOrder:     []string{"component", "category"},
+		HideKeys:        false,
+		NoFieldsSpace:   true,
+	})
+
+	ll := l.WithField("component", "main")
+	lll := ll.WithField("category", "rest")
+
+	l.Info("test1")
+	ll.Info("test2")
+	lll.Info("test3")
+
+	// Output:
+	// - [INFO] test1
+	// - [INFO][component:main] test2
+	// - [INFO][component:main][category:rest] test3
+}
+
 func ExampleFormatter_Format_trim_message() {
 	l := logrus.New()
 	l.SetOutput(os.Stdout)