Skip to content
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
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ tasks:
- .golangci.yml
preconditions:
- sh: command -v golangci-lint
msg: staticcheck not installed, run `brew install golangci-lint`
msg: golangci-lint not installed, run `brew install golangci-lint`

- sh: command -v typos
msg: requires typos-cli, run `brew install typos-cli`
Expand Down
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (l *Logger) clone() *Logger {
timeFunc: l.timeFunc,
timeFormat: l.timeFormat,
prefix: l.prefix,
attrs: l.attrs,
level: l.level,
mu: l.mu,
}
Expand Down Expand Up @@ -216,8 +217,7 @@ func putBuffer(buf *bytes.Buffer) {
// Approx 65kb
const maxSize = 64 << 10
if buf.Cap() > maxSize {
// Make the buffer nil so GC cleans it up
buf = nil
return
}

bufPool.Put(buf)
Expand Down
75 changes: 60 additions & 15 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func TestDebug(t *testing.T) {
}

func TestWith(t *testing.T) {
hue.Enabled(false) // Force no color

// Constantly return the same time
fixedTime := func() time.Time {
fixed, err := time.Parse(time.RFC3339, "2025-04-01T13:34:03Z")
Expand All @@ -157,24 +159,67 @@ func TestWith(t *testing.T) {

fixedTimeString := fixedTime().Format(time.RFC3339)

buf := &bytes.Buffer{}

logger := log.New(buf, log.TimeFunc(fixedTime))

logger.Info("I'm an info message")

sub := logger.With(
slog.Bool("sub", true),
slog.String("hello", "world"),
)
tests := []struct {
name string
fn func() string // Exercise the logger, return output
want string
}{
{
name: "attrs appear on sub logger",
fn: func() string {
buf := &bytes.Buffer{}
l := log.New(buf, log.TimeFunc(fixedTime))
l.Info("I'm an info message")
sub := l.With(slog.Bool("sub", true), slog.String("hello", "world"))
sub.Info("I'm also an info message")

return buf.String()
},
want: "[TIME] INFO: I'm an info message\n[TIME] INFO: I'm also an info message sub=true hello=world\n",
},
{
name: "chained With preserves earlier attrs",
fn: func() string {
buf := &bytes.Buffer{}
l := log.New(buf, log.TimeFunc(fixedTime))
l.With(slog.String("a", "1")).With(slog.String("b", "2")).Info("chained")

sub.Info("I'm also an info message")
return buf.String()
},
want: "[TIME] INFO: chained a=1 b=2\n",
},
{
name: "Prefixed preserves attrs from With",
fn: func() string {
buf := &bytes.Buffer{}
l := log.New(buf, log.TimeFunc(fixedTime))
l.With(slog.String("a", "1")).Prefixed("svc").Info("prefixed")

got := buf.String()
got = strings.TrimSpace(strings.ReplaceAll(got, fixedTimeString, "[TIME]")) + "\n"
return buf.String()
},
want: "[TIME] INFO svc: prefixed a=1\n",
},
{
name: "parent logger not affected by child With",
fn: func() string {
buf := &bytes.Buffer{}
l := log.New(buf, log.TimeFunc(fixedTime))
_ = l.With(slog.String("child", "attr"))
l.Info("parent should have no attrs")

return buf.String()
},
want: "[TIME] INFO: parent should have no attrs\n",
},
}

want := "[TIME] INFO: I'm an info message\n[TIME] INFO: I'm also an info message sub=true hello=world\n"
test.Diff(t, got, want)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fn()
got = strings.ReplaceAll(got, fixedTimeString, "[TIME]")
test.Diff(t, got, tt.want)
})
}
}

func TestRace(t *testing.T) {
Expand Down
Loading