Skip to content

Commit

Permalink
feat: add NoUppercaseLevel option (antonfisher#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfateyev authored Sep 19, 2020
1 parent b001633 commit d41190e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Formatter struct {
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
ShowFullLevel bool

// NoUppercaseLevel - no upper case for level value
NoUppercaseLevel bool

// TrimMessages - trim whitespaces on messages
TrimMessages bool

Expand Down
10 changes: 9 additions & 1 deletion formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Formatter struct {
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
ShowFullLevel bool

// NoUppercaseLevel - no upper case for level value
NoUppercaseLevel bool

// TrimMessages - trim whitespaces on messages
TrimMessages bool

Expand All @@ -60,7 +63,12 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
b.WriteString(entry.Time.Format(timestampFormat))

// write level
level := strings.ToUpper(entry.Level.String())
var level string
if f.NoUppercaseLevel {
level = entry.Level.String()
} else {
level = strings.ToUpper(entry.Level.String())
}

if f.CallerFirst {
f.writeCaller(b, entry)
Expand Down
27 changes: 27 additions & 0 deletions tests/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ func ExampleFormatter_Format_no_fields_space() {
// - [INFO][component:main][category:rest] test3
}

func ExampleFormatter_Format_no_uppercase_level() {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&formatter.Formatter{
NoColors: true,
TimestampFormat: "-",
FieldsOrder: []string{"component", "category"},
NoUppercaseLevel: true,
})

ll := l.WithField("component", "main")
lll := ll.WithField("category", "rest")
llll := ll.WithField("category", "other")

l.Debug("test1")
ll.Info("test2")
lll.Warn("test3")
llll.Error("test4")

// Output:
// - [debu] test1
// - [info] [component:main] test2
// - [warn] [component:main] [category:rest] test3
// - [erro] [component:main] [category:other] test4
}

func ExampleFormatter_Format_trim_message() {
l := logrus.New()
l.SetOutput(os.Stdout)
Expand Down

0 comments on commit d41190e

Please sign in to comment.