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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/FollowTheProcess/log

go 1.24

require github.com/FollowTheProcess/hue v0.5.2
require (
github.com/FollowTheProcess/hue v0.5.2
github.com/FollowTheProcess/test v0.21.0
)

require (
golang.org/x/sys v0.31.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
github.com/FollowTheProcess/hue v0.5.2 h1:Ns/vO8wiwv2oXDn+QyBLcYCK0LcfXVVu31bHGTLAFHI=
github.com/FollowTheProcess/hue v0.5.2/go.mod h1:5FD2UrxTzWi0Uc63w8ndsjqPrH4xn3Q7k7vEpINqEP4=
github.com/FollowTheProcess/snapshot v0.4.1 h1:WIwvs+yZrK9886frLbNB98QWkAXTjIGnuvEjURHOrtE=
github.com/FollowTheProcess/snapshot v0.4.1/go.mod h1:hb3V3azvp8FVrl4pvyhvq/ZYOnFSgyKHfXt96Jgg4e0=
github.com/FollowTheProcess/test v0.21.0 h1:8eljlAhdOusGXgEso8It6ymvk+Ua3MenNH3+4jp0+dY=
github.com/FollowTheProcess/test v0.21.0/go.mod h1:3UQ3s3EsAuD9Hw5aekXIxssqhIx7kCTjw2lKtYb0uFo=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
86 changes: 80 additions & 6 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/FollowTheProcess/hue"
"github.com/FollowTheProcess/log"
"github.com/FollowTheProcess/test"
)

func TestVisual(t *testing.T) {
hue.Enabled(true) // Force colour

logger := log.New(os.Stdout, log.WithLevel(log.LevelDebug))

logger.Debug("Doing some debuggy things")
Expand All @@ -22,27 +26,97 @@ func TestVisual(t *testing.T) {
logger.Error("File not found")
}

func TestDebug(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")
test.Ok(t, err)

return fixed
}

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

tests := []struct {
name string
msg string
want string
options []log.Option
}{
{
name: "disabled",
options: []log.Option{
log.WithLevel(log.LevelInfo),
},
msg: "You should not see me",
want: "", // Debug logs should not show up if the level is info
},
{
name: "enabled",
options: []log.Option{
log.WithLevel(log.LevelDebug),
},
msg: "Hello debug!",
want: "[TIME] DEBUG: Hello debug!\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}

// Ensure that the time is always deterministic
tt.options = append(tt.options, log.TimeFunc(fixedTime))

logger := log.New(buf, tt.options...)

logger.Debug(tt.msg)

got := buf.String()
got = strings.ReplaceAll(got, fixedTimeString, "[TIME]")

test.Diff(t, got, tt.want)
})
}
}

func TestRace(t *testing.T) {
buf := &bytes.Buffer{}
logger := log.New(buf)

// Constantly return the same time
fixedTime := func() time.Time {
fixed, err := time.Parse(time.RFC3339, "2025-04-01T13:34:03Z")
test.Ok(t, err)

return fixed
}

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

const n = 5

var wg sync.WaitGroup
wg.Add(n)
for range n {
go func(wg *sync.WaitGroup) {
for i := range n {
go func(wg *sync.WaitGroup, i int) {
defer wg.Done()
logger.Info(fmt.Sprintf("Something: %d", n))
}(&wg)
logger.Info(fmt.Sprintf("Something: %d", i))
}(&wg, i)
}

wg.Wait()

// Make sure they all got written, order doesn't matter because concurrency
got := strings.TrimSpace(buf.String())
lines := strings.Split(got, "\n")

test.Equal(t, len(lines), n, test.Context("expected %d log lines", n))
}

func BenchmarkLogger(b *testing.B) {
hue.Enabled(true) // Force colour
defer hue.Enabled(false)

b.Run("enabled", func(b *testing.B) {
buf := &bytes.Buffer{}
Expand Down
Loading