Skip to content

Commit 1940aa4

Browse files
committed
Enhance logging formatter tests
1 parent 6e3f871 commit 1940aa4

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

internal/logging/formatter.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Formatter struct{}
2121
// Format implements the log.Formatter interface.
2222
func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
2323
buff := &bytes.Buffer{}
24-
parts := []string{color.BlueString("lets:")}
24+
parts := []string{formatPrefix(entry)}
2525

2626
if data := writeData(entry.Data); data != "" {
2727
parts = append(parts, data)
@@ -35,8 +35,16 @@ func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
3535
return buff.Bytes(), nil
3636
}
3737

38+
func formatPrefix(entry *log.Entry) string {
39+
if entry.Level == log.DebugLevel {
40+
return color.BlueString("lets:")
41+
}
42+
43+
return "lets:"
44+
}
45+
3846
func formatMessage(entry *log.Entry) string {
39-
if entry.Level >= log.DebugLevel {
47+
if entry.Level == log.DebugLevel {
4048
return color.BlueString(entry.Message)
4149
}
4250

internal/logging/log_test.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,20 @@ func TestLoggingToStd(t *testing.T) {
5252
})
5353
}
5454

55-
func TestFormatterColorsVerboseMessages(t *testing.T) {
55+
func TestFormatterColorsDebugMessages(t *testing.T) {
5656
setNoColorForTest(t, false)
5757

58-
tests := []struct {
59-
name string
60-
level log.Level
61-
}{
62-
{name: "debug", level: log.DebugLevel},
63-
{name: "trace", level: log.TraceLevel},
58+
line, err := (&Formatter{}).Format(&log.Entry{
59+
Level: log.DebugLevel,
60+
Message: "debug message",
61+
})
62+
if err != nil {
63+
t.Fatalf("Format() error = %v", err)
6464
}
6565

66-
for _, tt := range tests {
67-
t.Run(tt.name, func(t *testing.T) {
68-
line, err := (&Formatter{}).Format(&log.Entry{
69-
Level: tt.level,
70-
Message: "debug message",
71-
})
72-
if err != nil {
73-
t.Fatalf("Format() error = %v", err)
74-
}
75-
76-
expected := color.BlueString("lets:") + " " + color.BlueString("debug message") + "\n"
77-
if string(line) != expected {
78-
t.Fatalf("unexpected debug line: %q", string(line))
79-
}
80-
})
66+
expected := color.BlueString("lets:") + " " + color.BlueString("debug message") + "\n"
67+
if string(line) != expected {
68+
t.Fatalf("unexpected debug line: %q", string(line))
8169
}
8270
}
8371

@@ -120,6 +108,14 @@ func TestFormatterFormatsLevelsAndFields(t *testing.T) {
120108
},
121109
fields: []string{"alpha=one", "beta=two"},
122110
},
111+
{
112+
name: "trace_no_data",
113+
entry: &log.Entry{
114+
Level: log.TraceLevel,
115+
Message: "trace message",
116+
Data: log.Fields{},
117+
},
118+
},
123119
}
124120

125121
for _, tt := range tests {

0 commit comments

Comments
 (0)