Skip to content

Commit

Permalink
Log wall-clock time when logging to a stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jul 18, 2024
1 parent 19be2ad commit c191d52
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ The format is based on [Keep a Changelog], and this project adheres to
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [0.2.2] - 2024-07-19

### Changed

- Display a wall-clock time (instead of the duration since the logger was
created) when logging to an `io.Writer`.'

## [0.2.1] - 2024-07-19

### Changed
Expand Down
32 changes: 13 additions & 19 deletions spruce.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"log/slog"
"slices"
"strings"
"time"
)

type handler struct {
log func(string) error
attrs []slog.Attr
groups []string
epoch time.Time
log func(string) error
attrs []slog.Attr
groups []string
writeTime func(*strings.Builder, slog.Record)
}

func (h *handler) Enabled(context.Context, slog.Level) bool {
Expand Down Expand Up @@ -53,15 +52,10 @@ func (h *handler) Handle(_ context.Context, rec slog.Record) error {

level := rec.Level.String()

elapsed := rec.Time.Sub(h.epoch)

buf.WriteByte('[')
buf.WriteString(level)
buf.WriteByte(' ')
if elapsed > 0 {
buf.WriteByte('+')
}
buf.WriteString(elapsed.String())
h.writeTime(buf, rec)

for i, g := range h.groups {
if i == 0 {
Expand Down Expand Up @@ -142,18 +136,18 @@ func writeAttrs(

func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &handler{
log: h.log,
attrs: append(slices.Clone(h.attrs), attrs...),
groups: h.groups,
epoch: h.epoch,
log: h.log,
attrs: append(slices.Clone(h.attrs), attrs...),
groups: h.groups,
writeTime: h.writeTime,
}
}

func (h *handler) WithGroup(name string) slog.Handler {
return &handler{
log: h.log,
attrs: h.attrs,
groups: append(slices.Clone(h.groups), name),
epoch: h.epoch,
log: h.log,
attrs: h.attrs,
groups: append(slices.Clone(h.groups), name),
writeTime: h.writeTime,
}
}
7 changes: 6 additions & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spruce
import (
"io"
"log/slog"
"strings"
"time"
)

Expand All @@ -25,6 +26,10 @@ func NewStreamHandler(w io.Writer) slog.Handler {
}
return nil
},
epoch: time.Now(),
writeTime: func(w *strings.Builder, rec slog.Record) {
w.WriteString(
rec.Time.Format(time.RFC3339),
)
},
}
}
11 changes: 10 additions & 1 deletion testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spruce

import (
"log/slog"
"strings"
"testing"
"time"
)
Expand All @@ -20,11 +21,19 @@ func NewTestLogger(t TestingT) *slog.Logger {

// NewTestHandler returns a new [slog.Handler] that writes to t.
func NewTestHandler(t TestingT) slog.Handler {
epoch := time.Now()

return &handler{
log: func(s string) error {
t.Log(s)
return nil
},
epoch: time.Now(),
writeTime: func(w *strings.Builder, rec slog.Record) {
elapsed := rec.Time.Sub(epoch)
if elapsed > 0 {
w.WriteByte('+')
}
w.WriteString(elapsed.String())
},
}
}

0 comments on commit c191d52

Please sign in to comment.