Skip to content

Commit d41190e

Browse files
authored
feat: add NoUppercaseLevel option (#6)
1 parent b001633 commit d41190e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Formatter struct {
3333
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
3434
ShowFullLevel bool
3535

36+
// NoUppercaseLevel - no upper case for level value
37+
NoUppercaseLevel bool
38+
3639
// TrimMessages - trim whitespaces on messages
3740
TrimMessages bool
3841

formatter.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type Formatter struct {
3434
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
3535
ShowFullLevel bool
3636

37+
// NoUppercaseLevel - no upper case for level value
38+
NoUppercaseLevel bool
39+
3740
// TrimMessages - trim whitespaces on messages
3841
TrimMessages bool
3942

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

6265
// write level
63-
level := strings.ToUpper(entry.Level.String())
66+
var level string
67+
if f.NoUppercaseLevel {
68+
level = entry.Level.String()
69+
} else {
70+
level = strings.ToUpper(entry.Level.String())
71+
}
6472

6573
if f.CallerFirst {
6674
f.writeCaller(b, entry)

tests/formatter_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,33 @@ func ExampleFormatter_Format_no_fields_space() {
168168
// - [INFO][component:main][category:rest] test3
169169
}
170170

171+
func ExampleFormatter_Format_no_uppercase_level() {
172+
l := logrus.New()
173+
l.SetOutput(os.Stdout)
174+
l.SetLevel(logrus.DebugLevel)
175+
l.SetFormatter(&formatter.Formatter{
176+
NoColors: true,
177+
TimestampFormat: "-",
178+
FieldsOrder: []string{"component", "category"},
179+
NoUppercaseLevel: true,
180+
})
181+
182+
ll := l.WithField("component", "main")
183+
lll := ll.WithField("category", "rest")
184+
llll := ll.WithField("category", "other")
185+
186+
l.Debug("test1")
187+
ll.Info("test2")
188+
lll.Warn("test3")
189+
llll.Error("test4")
190+
191+
// Output:
192+
// - [debu] test1
193+
// - [info] [component:main] test2
194+
// - [warn] [component:main] [category:rest] test3
195+
// - [erro] [component:main] [category:other] test4
196+
}
197+
171198
func ExampleFormatter_Format_trim_message() {
172199
l := logrus.New()
173200
l.SetOutput(os.Stdout)

0 commit comments

Comments
 (0)