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

Multiline support #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var attrs = []slog.Attr{
slog.Any("err", errors.New("yo")),
slog.Group("empty"),
slog.Group("group", slog.String("bar", "baz")),
slog.String("multi", "foo\nbar"),
}

var attrsAny = func() (a []any) {
Expand Down
4 changes: 4 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (b *buffer) Cap() int {
}

func (b *buffer) WriteTo(dst io.Writer) (int64, error) {
if b == nil {
// for convenience, if receiver is nil, treat it like an empty buffer
return 0, nil
}
l := len(*b)
if l == 0 {
return 0, nil
Expand Down
9 changes: 9 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func TestBuffer_WriteTo(t *testing.T) {
AssertNoError(t, err)
AssertEqual(t, "foobar", dest.String())
AssertZero(t, b.Len())

t.Run("nilbuffer", func(t *testing.T) {
// if receiver is nil, do nothing
dest.Reset()
c, err := (*buffer)(nil).WriteTo(&dest)
AssertNoError(t, err)
AssertZero(t, c)
AssertZero(t, dest.Len())
})
}

func TestBuffer_Clone(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (e encoder) writeAttr(buf *buffer, a slog.Attr, group string) {
}
return
}

buf.AppendByte(' ')
e.withColor(buf, e.opts.Theme.AttrKey(), func() {
if group != "" {
Expand Down
47 changes: 42 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package console

import (
"bytes"
"context"
"io"
"log/slog"
Expand All @@ -14,6 +15,17 @@ var bufferPool = &sync.Pool{
New: func() any { return new(buffer) },
}

func getBuf() *buffer {
return bufferPool.Get().(*buffer)
}

func releaseBuf(buf *buffer) {
if buf != nil {
buf.Reset()
bufferPool.Put(buf)
}
}

var cwd, _ = os.Getwd()

// HandlerOptions are options for a ConsoleHandler.
Expand Down Expand Up @@ -82,7 +94,8 @@ func (h *Handler) Enabled(_ context.Context, l slog.Level) bool {

// Handle implements slog.Handler.
func (h *Handler) Handle(_ context.Context, rec slog.Record) error {
buf := bufferPool.Get().(*buffer)
buf := getBuf()
var multiLineBuf *buffer

h.enc.writeTimestamp(buf, rec.Time)
h.enc.writeLevel(buf, rec.Level)
Expand All @@ -92,16 +105,40 @@ func (h *Handler) Handle(_ context.Context, rec slog.Record) error {
h.enc.writeMessage(buf, rec.Level, rec.Message)
buf.copy(&h.context)
rec.Attrs(func(a slog.Attr) bool {
idx := buf.Len()
h.enc.writeAttr(buf, a, h.group)
lastAttr := (*buf)[idx:]
if bytes.IndexByte(lastAttr, '\n') >= 0 {
if multiLineBuf == nil {
multiLineBuf = getBuf()
multiLineBuf.AppendByte(' ')
}
if k, v, ok := bytes.Cut(lastAttr, []byte("=")); ok {
multiLineBuf.Append(k[1:])
multiLineBuf.AppendByte('=')
multiLineBuf.AppendByte('\n')
multiLineBuf.Append(v)
multiLineBuf.AppendByte('\n')
} else {
multiLineBuf.Append(lastAttr[1:])
multiLineBuf.AppendByte('\n')
}

*buf = (*buf)[:idx]
}
return true
})
h.enc.NewLine(buf)
if multiLineBuf == nil {
h.enc.NewLine(buf)
}
if _, err := buf.WriteTo(h.out); err != nil {
buf.Reset()
bufferPool.Put(buf)
return err
}
bufferPool.Put(buf)
if _, err := multiLineBuf.WriteTo(h.out); err != nil {
return err
}
releaseBuf(buf)
releaseBuf(multiLineBuf)
return nil
}

Expand Down
67 changes: 67 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,73 @@ func TestHandler_Attr(t *testing.T) {
AssertEqual(t, expected, buf.String())
}

func TestHandler_AttrsWithNewlines(t *testing.T) {
tests := []struct {
name string
msg string
escapeNewlines bool
attrs []slog.Attr
want string
}{
{
name: "single attr",
attrs: []slog.Attr{
slog.String("foo", "line one\nline two"),
},
want: "INF multiline attrs foo=\nline one\nline two\n",
},
{
name: "multiple attrs",
attrs: []slog.Attr{
slog.String("foo", "line one\nline two"),
slog.String("bar", "line three\nline four"),
},
want: "INF multiline attrs foo=\nline one\nline two\nbar=\nline three\nline four\n",
},
{
name: "sort multiline attrs to end",
attrs: []slog.Attr{
slog.String("size", "big"),
slog.String("foo", "line one\nline two"),
slog.String("weight", "heavy"),
slog.String("bar", "line three\nline four"),
slog.String("color", "red"),
},
want: "INF multiline attrs size=big weight=heavy color=red foo=\nline one\nline two\nbar=\nline three\nline four\n",
},
{
name: "multiline message",
msg: "multiline\nmessage",
want: "INF multiline\nmessage\n",
},
{
name: "preserve leading and trailing newlines",
attrs: []slog.Attr{
slog.String("foo", "\nline one\nline two\n"),
},
want: "INF multiline attrs foo=\n\nline one\nline two\n\n",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := bytes.Buffer{}
h := NewHandler(&buf, &HandlerOptions{NoColor: true})

msg := test.msg
if msg == "" {
msg = "multiline attrs"
}
rec := slog.NewRecord(time.Time{}, slog.LevelInfo, msg, 0)
rec.AddAttrs(test.attrs...)
AssertNoError(t, h.Handle(context.Background(), rec))

AssertEqual(t, test.want, buf.String())
})

}
}

// Handlers should not log groups (or subgroups) without fields.
// '- If a group has no Attrs (even if it has a non-empty key), ignore it.'
// https://pkg.go.dev/log/slog@master#Handler
Expand Down
Loading