Skip to content

Commit

Permalink
Add option NoFieldsSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-ywliu committed Aug 29, 2020
1 parent 28a283d commit 7c58140
Show file tree
Hide file tree
Showing 2 changed files with 21 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

0 comments on commit 7c58140

Please sign in to comment.