Skip to content

feat: modify slog.Logger to *not* satisfy slog.Sink #95

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

Merged
merged 6 commits into from
Mar 4, 2021
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 example_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func httpLogHelper(ctx context.Context, status int) {
)
}

var l = sloghuman.Make(os.Stdout)
var l = slog.Make(sloghuman.Sink(os.Stdout))

func ExampleHelper() {
ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion example_marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s myStruct) MarshalJSON() ([]byte, error) {
}

func Example_marshaller() {
l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))

l.Info(context.Background(), "wow",
slog.F("myStruct", myStruct{
Expand Down
18 changes: 9 additions & 9 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func Example() {
log := sloghuman.Make(os.Stdout)
log := slog.Make(sloghuman.Sink(os.Stdout))

log.Info(context.Background(), "my message here",
slog.F("field_name", "something or the other"),
Expand All @@ -45,7 +45,7 @@ func Example() {
}

func Example_struct() {
l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))

type hello struct {
Meow int `json:"meow"`
Expand Down Expand Up @@ -76,7 +76,7 @@ func Example_testing() {
}

func Example_tracing() {
log := sloghuman.Make(os.Stdout)
log := slog.Make(sloghuman.Sink(os.Stdout))

ctx, _ := trace.StartSpan(context.Background(), "spanName")

Expand All @@ -86,14 +86,14 @@ func Example_tracing() {
}

func Example_multiple() {
l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))

f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
}

l = slog.Make(l, slogstackdriver.Make(f))
l = l.AppendSinks(slogstackdriver.Sink(f))

l.Info(context.Background(), "log to stdout and stackdriver")

Expand All @@ -103,15 +103,15 @@ func Example_multiple() {
func ExampleWith() {
ctx := slog.With(context.Background(), slog.F("field", 1))

l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))
l.Info(ctx, "msg")

// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
}

func ExampleStdlib() {
ctx := slog.With(context.Background(), slog.F("field", 1))
l := slog.Stdlib(ctx, sloghuman.Make(os.Stdout))
l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout)))

l.Print("msg")

Expand All @@ -121,7 +121,7 @@ func ExampleStdlib() {
func ExampleLogger_Named() {
ctx := context.Background()

l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))
l = l.Named("http")
l.Info(ctx, "received request", slog.F("remote address", net.IPv4(127, 0, 0, 1)))

Expand All @@ -131,7 +131,7 @@ func ExampleLogger_Named() {
func ExampleLogger_Leveled() {
ctx := context.Background()

l := sloghuman.Make(os.Stdout)
l := slog.Make(sloghuman.Sink(os.Stdout))
l.Debug(ctx, "testing1")
l.Info(ctx, "received request")

Expand Down
28 changes: 0 additions & 28 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestStdlib(t *testing.T) {
t.Parallel()

b := &bytes.Buffer{}
l := slog.Make(sloghuman.Make(b)).With(
l := slog.Make(sloghuman.Sink(b)).With(
slog.F("hi", "we"),
)
stdlibLog := slog.Stdlib(bg, l)
Expand Down
18 changes: 10 additions & 8 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type Sink interface {
Sync()
}

// LogEntry logs the given entry with the context to the
// Log logs the given entry with the context to the
// underlying sinks.
//
// It extends the entry with the set fields and names.
func (l Logger) LogEntry(ctx context.Context, e SinkEntry) {
func (l Logger) Log(ctx context.Context, e SinkEntry) {
if e.Level < l.level {
return
}
Expand Down Expand Up @@ -138,17 +138,19 @@ func (l Logger) Named(name string) Logger {
func (l Logger) Leveled(level Level) Logger {
l.level = level
l.sinks = append([]Sink(nil), l.sinks...)
for i, s := range l.sinks {
if l2, ok := s.(Logger); ok {
l.sinks[i] = l2.Leveled(level)
}
}
return l
}

// AppendSinks appends the sinks to the set sink
// targets on the logger.
func (l Logger) AppendSinks(s ...Sink) Logger {
l.sinks = append(l.sinks, s...)
return l
}

func (l Logger) log(ctx context.Context, level Level, msg string, fields Map) {
ent := l.entry(ctx, level, msg, fields)
l.LogEntry(ctx, ent)
l.Log(ctx, ent)
}

func (l Logger) entry(ctx context.Context, level Level, msg string, fields Map) SinkEntry {
Expand Down
7 changes: 4 additions & 3 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func TestLogger(t *testing.T) {

s1 := &fakeSink{}
s2 := &fakeSink{}
l := slog.Make(s1, s2)
l := slog.Make(s1)
l = l.Leveled(slog.LevelError)
l = l.AppendSinks(s2)

l.Info(bg, "wow", slog.Error(io.EOF))
l.Error(bg, "meow", slog.Error(io.ErrUnexpectedEOF))
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestLogger(t *testing.T) {

File: slogTestFile,
Func: "cdr.dev/slog_test.TestLogger.func2",
Line: 66,
Line: 67,

Fields: slog.M(
slog.F("ctx", 1024),
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestLogger(t *testing.T) {

File: slogTestFile,
Func: "cdr.dev/slog_test.TestLogger.func3",
Line: 97,
Line: 98,

SpanContext: span.SpanContext(),

Expand Down
8 changes: 4 additions & 4 deletions sloggers/sloghuman/sloghuman.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"cdr.dev/slog/internal/syncwriter"
)

// Make creates a logger that writes logs in a human
// Sink creates a slog.Sink that writes logs in a human
// readable YAML like format to the given writer.
//
// If the writer implements Sync() error then
// it will be called when syncing.
func Make(w io.Writer) slog.Logger {
return slog.Make(&humanSink{
func Sink(w io.Writer) slog.Sink {
return &humanSink{
w: syncwriter.New(w),
w2: w,
})
}
}

type humanSink struct {
Expand Down
2 changes: 1 addition & 1 deletion sloggers/sloghuman/sloghuman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestMake(t *testing.T) {
t.Parallel()

b := &bytes.Buffer{}
l := sloghuman.Make(b)
l := slog.Make(sloghuman.Sink(b))
l.Info(bg, "line1\n\nline2", slog.F("wowow", "me\nyou"))
l.Sync()

Expand Down
4 changes: 2 additions & 2 deletions sloggers/slogjson/slogjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
"cdr.dev/slog/internal/syncwriter"
)

// Make creates a logger that writes JSON logs
// Sink creates a slog.Sink that writes JSON logs
// to the given writer. See package level docs
// for the format.
// If the writer implements Sync() error then
// it will be called when syncing.
func Make(w io.Writer) slog.Logger {
func Sink(w io.Writer) slog.Logger {
return slog.Make(jsonSink{
w: syncwriter.New(w),
})
Expand Down
2 changes: 1 addition & 1 deletion sloggers/slogjson/slogjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMake(t *testing.T) {

ctx, s := trace.StartSpan(bg, "meow")
b := &bytes.Buffer{}
l := slogjson.Make(b)
l := slogjson.Sink(b)
l = l.Named("named")
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))

Expand Down
8 changes: 4 additions & 4 deletions sloggers/slogstackdriver/slogstackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
"cdr.dev/slog/internal/syncwriter"
)

// Make creates a slog.Logger configured to write JSON logs
// Sink creates a slog.Sink configured to write JSON logs
// to stdout for stackdriver.
//
// See https://cloud.google.com/logging/docs/agent
func Make(w io.Writer) slog.Logger {
func Sink(w io.Writer) slog.Sink {
projectID, _ := metadata.ProjectID()

return slog.Make(stackdriverSink{
return stackdriverSink{
projectID: projectID,
w: syncwriter.New(w),
})
}
}

type stackdriverSink struct {
Expand Down
2 changes: 1 addition & 1 deletion sloggers/slogstackdriver/slogstackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestStackdriver(t *testing.T) {

ctx, s := trace.StartSpan(bg, "meow")
b := &bytes.Buffer{}
l := slogstackdriver.Make(b)
l := slog.Make(slogstackdriver.Sink(b))
l = l.Named("meow")
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))

Expand Down
2 changes: 1 addition & 1 deletion sloggers/slogtest/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// Ensure all stdlib logs go through slog.
func init() {
l := sloghuman.Make(os.Stderr)
l := slog.Make(sloghuman.Sink(os.Stderr))
log.SetOutput(slog.Stdlib(context.Background(), l).Writer())
}

Expand Down
5 changes: 2 additions & 3 deletions sloggers/slogtest/t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/sloggers/slogtest"
)
Expand All @@ -31,9 +30,9 @@ func TestIgnoreErrors(t *testing.T) {
t.Parallel()

tb := &fakeTB{}
l := slog.Make(slogtest.Make(tb, &slogtest.Options{
l := slogtest.Make(tb, &slogtest.Options{
IgnoreErrors: true,
}))
})

l.Error(bg, "hello")
assert.Equal(t, "errors", 0, tb.errors)
Expand Down