Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Empty Messages #82

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ examples/new/new
examples/options/options
examples/oven/oven
examples/temperature/temperature

.vscode
.history
go.work
6 changes: 4 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ func (l *Logger) log(level Level, msg interface{}, keyvals ...interface{}) {
}

if msg != nil {
m := fmt.Sprint(msg)
kvs = append(kvs, MessageKey, m)
if m := fmt.Sprint(msg); m != "" {
kvs = append(kvs, MessageKey, m)
}
}

// append logger fields
kvs = append(kvs, l.fields...)
if len(l.fields)%2 != 0 {
kvs = append(kvs, ErrMissingValue)
}

// append the rest
kvs = append(kvs, keyvals...)
if len(keyvals)%2 != 0 {
Expand Down
41 changes: 41 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,47 @@ func TestLogFormatter(t *testing.T) {
}
}

func TestEmptyMessage(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
cases := []struct {
name string
expected string
msg string
fields []interface{}
kvs []interface{}
}{
{
name: "empty message nil fields",
expected: "INFO\n",
msg: "",
fields: nil,
kvs: nil,
},
{
name: "empty message with fields",
expected: "INFO foo=bar\n",
msg: "",
fields: []interface{}{"foo", "bar"},
kvs: nil,
},
{
name: "empty message with fields & kvs",
expected: "INFO foo=bar foobar=baz\n",
msg: "",
fields: []interface{}{"foo", "bar"},
kvs: []interface{}{"foobar", "baz"},
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
l.With(c.fields...).Info(c.msg, c.kvs...)
assert.Equal(t, c.expected, buf.String())
})
}
}

func TestLogWithPrefix(t *testing.T) {
var buf bytes.Buffer
cases := []struct {
Expand Down
37 changes: 26 additions & 11 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,61 @@
}

func (l *Logger) textFormatter(keyvals ...interface{}) {
for i := 0; i < len(keyvals); i += 2 {
lenKeyvals := len(keyvals)

for i := 0; i < lenKeyvals; i += 2 {
firstKey := i == 0
moreKeys := i < lenKeyvals-2

switch keyvals[i] {
case TimestampKey:
if t, ok := keyvals[i+1].(time.Time); ok {
ts := t.Format(l.timeFormat)
ts = TimestampStyle.Renderer(l.re).Render(ts)
if !firstKey {
l.b.WriteByte(' ')
}

Check warning on line 159 in text.go

View check run for this annotation

Codecov / codecov/patch

text.go#L158-L159

Added lines #L158 - L159 were not covered by tests
Comment on lines +157 to +159
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps put this in a helper function writeSpace(bool)

l.b.WriteString(ts)
l.b.WriteByte(' ')
}
case LevelKey:
if level, ok := keyvals[i+1].(Level); ok {
lvl := levelStyle(level).Renderer(l.re).String()
if !firstKey {
l.b.WriteByte(' ')
}
l.b.WriteString(lvl)
l.b.WriteByte(' ')
}
case CallerKey:
if caller, ok := keyvals[i+1].(string); ok {
caller = fmt.Sprintf("<%s>", caller)
caller = CallerStyle.Renderer(l.re).Render(caller)
if !firstKey {
l.b.WriteByte(' ')
}
l.b.WriteString(caller)
l.b.WriteByte(' ')
}
case PrefixKey:
if prefix, ok := keyvals[i+1].(string); ok {
prefix = PrefixStyle.Renderer(l.re).Render(prefix + ":")
if !firstKey {
l.b.WriteByte(' ')
}
l.b.WriteString(prefix)
l.b.WriteByte(' ')
}
case MessageKey:
if msg := keyvals[i+1]; msg != nil {
m := fmt.Sprint(msg)
m = MessageStyle.Renderer(l.re).Render(m)
if !firstKey {
l.b.WriteByte(' ')
}
l.b.WriteString(m)
}
default:
sep := separator
indentSep := indentSeparator
sep = SeparatorStyle.Renderer(l.re).Render(sep)
indentSep = SeparatorStyle.Renderer(l.re).Render(indentSep)
moreKeys := i < len(keyvals)-2
key := fmt.Sprint(keyvals[i])
val := fmt.Sprintf("%+v", keyvals[i+1])
raw := val == ""
Expand Down Expand Up @@ -215,19 +230,19 @@
l.b.WriteString(key)
l.b.WriteString(sep + "\n")
l.writeIndent(&l.b, val, indentSep, moreKeys, actualKey)
// If there are more keyvals, separate them with a space.
if moreKeys {
} else if !raw && needsQuoting(val) {
if !firstKey {
l.b.WriteByte(' ')
}
} else if !raw && needsQuoting(val) {
l.b.WriteByte(' ')
l.b.WriteString(key)
l.b.WriteString(sep)
l.b.WriteString(valueStyle.Renderer(l.re).Render(fmt.Sprintf(`"%s"`,
escapeStringForOutput(val, true))))
} else {
val = valueStyle.Renderer(l.re).Render(val)
l.b.WriteByte(' ')
if !firstKey {
l.b.WriteByte(' ')
}
l.b.WriteString(key)
l.b.WriteString(sep)
l.b.WriteString(val)
Expand Down