generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package spruce | ||
|
||
import ( | ||
"io" | ||
"log/slog" | ||
"time" | ||
) | ||
|
||
var newLine = []byte{'\n'} | ||
|
||
// NewStreamLogger returns a [slog.Logger] that writes to w. | ||
func NewStreamLogger(w io.Writer) *slog.Logger { | ||
return slog.New(NewStreamHandler(w)) | ||
} | ||
|
||
// NewStreamHandler returns a new [slog.Handler] that writes to w. | ||
func NewStreamHandler(w io.Writer) slog.Handler { | ||
return &handler{ | ||
log: func(s string) error { | ||
if _, err := w.Write([]byte(s)); err != nil { | ||
return err | ||
} | ||
if _, err := w.Write(newLine); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
epoch: time.Now(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package spruce_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/dogmatiq/spruce" | ||
) | ||
|
||
func TestNewStreamLogger(t *testing.T) { | ||
w := &strings.Builder{} | ||
l := NewStreamLogger(w) | ||
|
||
l.Info("<message>") | ||
|
||
got := w.String() | ||
want := "<message>\n" | ||
|
||
if !strings.HasSuffix(got, want) { | ||
t.Errorf("got %q, want suffix of %q", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package spruce | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestingT is the subset of [testing.TB] that is used to write logs. | ||
type TestingT interface { | ||
Log(...any) | ||
} | ||
|
||
var _ TestingT = (testing.TB)(nil) | ||
|
||
// NewTestLogger returns a [slog.Logger] that writes to t. | ||
func NewTestLogger(t TestingT) *slog.Logger { | ||
return slog.New(NewTestHandler(t)) | ||
} | ||
|
||
// NewTestHandler returns a new [slog.Handler] that writes to t. | ||
func NewTestHandler(t TestingT) slog.Handler { | ||
return &handler{ | ||
log: func(s string) error { | ||
t.Log(s) | ||
return nil | ||
}, | ||
epoch: time.Now(), | ||
} | ||
} |