Skip to content

Commit

Permalink
feat: add NoFieldsSpace option (antonfisher#5)
Browse files Browse the repository at this point in the history
* Add option NoFieldsSpace

* tests: add tests for NoFieldsSpace option
  • Loading branch information
tim-ywliu authored Sep 3, 2020
1 parent 28a283d commit b001633
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 18 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
25 changes: 25 additions & 0 deletions tests/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b001633

Please sign in to comment.