Skip to content

Commit

Permalink
feat: add NoFieldsColors option, fields with colors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfisher committed Oct 20, 2018
1 parent f0afcd9 commit b7ed065
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ Default logrus formatter:

```go
type Formatter struct {
FieldsOrder []string // default: fields sorted alphabetically
TimestampFormat string // default: time.StampMilli = "Jan _2 15:04:05.000"
HideKeys bool // show [fieldValue] instead of [fieldKey:fieldValue]
NoColors bool // disable colors
ShowFullLevel bool // true to show full level [WARNING] instead of [WARN]
FieldsOrder []string // default: fields sorted alphabetically
NoFieldsColors bool // color only level, default is level + fields
ShowFullLevel bool // true to show full level [WARNING] instead [WARN]
}
```

Expand Down
8 changes: 4 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func main() {
printDemo(&formatter.Formatter{
HideKeys: true,
FieldsOrder: []string{"component", "category", "req"},
})
}, "nested-logrus-formatter")

fmt.Print("\n--- default logrus formatter ---\n\n")
printDemo(nil)
printDemo(nil, "default logrus formatter")
}

func printDemo(f logrus.Formatter) {
func printDemo(f logrus.Formatter, title string) {
l := logrus.New()

l.SetLevel(logrus.DebugLevel)
Expand All @@ -26,7 +26,7 @@ func printDemo(f logrus.Formatter) {
l.SetFormatter(f)
}

l.Info("this is nested-logrus-formatter demo")
l.Infof("this is %v demo", title)

lWebServer := l.WithField("component", "web-server")
lWebServer.Info("starting...")
Expand Down
21 changes: 12 additions & 9 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

// Formatter - logrus formatter, implements logrus.Formatter
type Formatter struct {
FieldsOrder []string // default: fields sorted alphabetically
TimestampFormat string // default: time.StampMilli = "Jan _2 15:04:05.000"
HideKeys bool // show [fieldValue] instead of [fieldKey:fieldValue]
NoColors bool // disable colors
NoFieldsColors bool // color only level, default is level + fields
ShowFullLevel bool // true to show full level [WARNING] instead [WARN]
FieldsOrder []string // default: fields sorted alphabetically
}

// Format an log entry
Expand All @@ -37,22 +38,20 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
// write level
level := strings.ToUpper(entry.Level.String())

if f.NoColors {
b.WriteString(" [")
} else {
fmt.Fprintf(b, " \x1b[%dm[", levelColor)
if !f.NoColors {
fmt.Fprintf(b, "\x1b[%dm", levelColor)
}

b.WriteString(" [")
if f.ShowFullLevel {
b.WriteString(level)
} else {
b.WriteString(level[:4])
}
b.WriteString("] ")

if f.NoColors {
b.WriteString("] ")
} else {
b.WriteString("]\x1b[0m ")
if !f.NoColors && f.NoFieldsColors {
b.WriteString("\x1b[0m ")
}

// write fields
Expand All @@ -62,6 +61,10 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
f.writeOrderedFields(b, entry)
}

if !f.NoColors && !f.NoFieldsColors {
b.WriteString("\x1b[0m ")
}

// write message
b.WriteString(entry.Message)
b.WriteByte('\n')
Expand Down

0 comments on commit b7ed065

Please sign in to comment.