diff --git a/README.md b/README.md index befb071..79317ef 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/formatter.go b/formatter.go index 2c5daf7..8defcb8 100644 --- a/formatter.go +++ b/formatter.go @@ -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 @@ -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) diff --git a/tests/formatter_test.go b/tests/formatter_test.go index dcc85d0..6c0286b 100644 --- a/tests/formatter_test.go +++ b/tests/formatter_test.go @@ -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)