Skip to content

Tee #87

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

Closed
wants to merge 2 commits into from
Closed

Tee #87

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
20 changes: 2 additions & 18 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,8 @@ import "context"

type loggerCtxKey = struct{}

type sinkContext struct {
context.Context
Sink
}

// SinkContext is a context that implements Sink.
// It may be returned by log creators to allow for composition.
type SinkContext interface {
Sink
context.Context
}

func contextWithLogger(ctx context.Context, l logger) SinkContext {
ctx = context.WithValue(ctx, loggerCtxKey{}, l)
return &sinkContext{
Context: ctx,
Sink: l,
}
func contextWithLogger(ctx context.Context, l logger) context.Context {
return context.WithValue(ctx, loggerCtxKey{}, l)
}

func loggerFromContext(ctx context.Context) (logger, bool) {
Expand Down
4 changes: 2 additions & 2 deletions example_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"os"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/sloggers/sloghuman"
)

func httpLogHelper(ctx context.Context, status int) {
Expand Down
4 changes: 2 additions & 2 deletions example_marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/sloggers/sloghuman"
)

type myStruct struct {
Expand Down
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"go.opencensus.io/trace"
"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogstackdriver"
"cdr.dev/slog/sloggers/slogtest"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/sloggers/sloghuman"
"cdr.dev/slog/v2/sloggers/slogstackdriver"
"cdr.dev/slog/v2/sloggers/slogtest"
)

func Example() {
Expand Down Expand Up @@ -94,7 +94,7 @@ func Example_multiple() {
slog.Fatal(ctx, "failed to open stackdriver log file", slog.Err(err))
}

ctx = slog.Make(l, slogstackdriver.Make(ctx, f))
ctx = slog.Tee(l, slogstackdriver.Make(ctx, f))

slog.Info(ctx, "log to stdout and stackdriver")

Expand Down
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ module cdr.dev/slog/v2
go 1.13

require (
cdr.dev/slog v1.3.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be here.

cloud.google.com/go v0.49.0
github.com/alecthomas/chroma v0.7.0
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/fatih/color v1.7.0
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
github.com/google/go-cmp v0.3.2-0.20191216170541-340f1ebe299e
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.11 // indirect
go.opencensus.io v0.22.2
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
google.golang.org/grpc v1.25.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cdr.dev/slog v1.3.0 h1:MYN1BChIaVEGxdS7I5cpdyMC0+WfJfK8BETAfzfLUGQ=
cdr.dev/slog v1.3.0/go.mod h1:C5OL99WyuOK8YHZdYY57dAPN1jK2WJlCdq2VP6xeQns=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down
2 changes: 1 addition & 1 deletion internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/v2"
)

// StripTimestamp strips the timestamp from entry and returns
Expand Down
6 changes: 3 additions & 3 deletions internal/entryhuman/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
"cdr.dev/slog/v2/internal/entryhuman"
)

var kt = time.Date(2000, time.February, 5, 4, 4, 4, 4, time.UTC)
Expand Down
2 changes: 1 addition & 1 deletion internal/syncwriter/syncwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"cdr.dev/slog/internal/assert"
"cdr.dev/slog/v2/internal/assert"
)

type testWriter struct {
Expand Down
10 changes: 5 additions & 5 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
)

var _, mapTestFile, _, _ = runtime.Caller(0)
Expand Down Expand Up @@ -61,12 +61,12 @@ func TestMap(t *testing.T) {
"error": [
{
"msg": "wrap1",
"fun": "cdr.dev/slog_test.TestMap.func2",
"fun": "cdr.dev/slog/v2_test.TestMap.func2",
"loc": "`+mapTestFile+`:41"
},
{
"msg": "wrap2",
"fun": "cdr.dev/slog_test.TestMap.func2",
"fun": "cdr.dev/slog/v2_test.TestMap.func2",
"loc": "`+mapTestFile+`:42"
},
"EOF"
Expand All @@ -92,7 +92,7 @@ func TestMap(t *testing.T) {
"error": [
{
"msg": "failed to marshal to JSON",
"fun": "cdr.dev/slog.encodeJSON",
"fun": "cdr.dev/slog/v2.encodeJSON",
"loc": "`+mapTestFile+`:131"
},
"json: error calling MarshalJSON for type slog_test.complexJSON: json: unsupported type: complex128"
Expand Down
10 changes: 5 additions & 5 deletions s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"context"
"testing"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
"cdr.dev/slog/v2/internal/entryhuman"
"cdr.dev/slog/v2/sloggers/sloghuman"
)

func TestStdlib(t *testing.T) {
t.Parallel()

b := &bytes.Buffer{}
ctx := context.Background()
ctx = slog.Make(sloghuman.Make(ctx, b))
ctx = slog.Tee(sloghuman.Make(ctx, b))
ctx = slog.With(ctx,
slog.F("hi", "we"),
)
Expand Down
37 changes: 26 additions & 11 deletions slog.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Package slog implements minimal structured logging.
//
// See https://cdr.dev/slog for overview docs and a comparison with existing libraries.
// See https://cdr.dev/slog/v2 for overview docs and a comparison with existing libraries.
//
// The examples are the best way to understand how to use this library effectively.
//
// The logger type implements a high level API around the Sink interface.
// logger implements Sink as well to allow composition.
//
// Implementations of the Sink interface are available in the sloggers subdirectory.
package slog // import "cdr.dev/slog"
package slog // import "cdr.dev/slog/v2"

import (
"context"
Expand Down Expand Up @@ -77,17 +77,32 @@ type logger struct {
exit func(int)
}

// Make creates a logger that writes logs to the passed sinks at LevelInfo.
func Make(ctx context.Context, sinks ...Sink) SinkContext {
// Just in case the ctx has a logger, start with it.
l, _ := loggerFromContext(ctx)
l.sinks = append(l.sinks, sinks...)
if l.level == 0 {
l.level = LevelInfo
// Make returns a context with the logger for sink.
func Make(ctx context.Context, sink Sink) context.Context {
return contextWithLogger(ctx, logger{
sinks: []Sink{sink},
level: LevelInfo,
exit: os.Exit,
})
}

// Tee creates a logger that writes logs to the passed sinks at LevelInfo.
// The first context is preserved in the return.
func Tee(ctx context.Context, ctxs ...context.Context) context.Context {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about this anymore.

var sinks []Sink
for _, c := range append([]context.Context{ctx}, ctxs...) {
l, ok := loggerFromContext(c)
if !ok {
continue
}
sinks = append(sinks, l)
}
l.exit = os.Exit

return contextWithLogger(ctx, l)
return contextWithLogger(ctx, logger{
sinks: sinks,
level: LevelInfo,
exit: os.Exit,
})
}

// Debug logs the msg and fields at LevelDebug.
Expand Down
12 changes: 6 additions & 6 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
)

var _, slogTestFile, _, _ = runtime.Caller(0)
Expand All @@ -36,8 +36,8 @@ func TestLogger(t *testing.T) {

s1 := &fakeSink{}
s2 := &fakeSink{}
var ctx context.Context
ctx = slog.Make(context.Background(), s1, s2)
ctx := context.Background()
ctx = slog.Tee(ctx, slog.Make(ctx, s1), slog.Make(ctx, s2))
ctx = slog.Leveled(ctx, slog.LevelError)

slog.Info(ctx, "wow", slog.Err(io.EOF))
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestLogger(t *testing.T) {
Message: "logging in helper",

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

Fields: slog.M(
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestLogger(t *testing.T) {
LoggerNames: []string{"hello", "hello2"},

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

SpanContext: span.SpanContext(),
Expand Down
10 changes: 5 additions & 5 deletions sloggers/sloghuman/sloghuman.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Package sloghuman contains the slogger
// that writes logs in a human readable format.
package sloghuman // import "cdr.dev/slog/sloggers/sloghuman"
package sloghuman // import "cdr.dev/slog/v2/sloggers/sloghuman"

import (
"context"
"io"
"strings"

"cdr.dev/slog"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/internal/syncwriter"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/entryhuman"
"cdr.dev/slog/v2/internal/syncwriter"
)

// Make creates a logger 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(ctx context.Context, w io.Writer) slog.SinkContext {
func Make(ctx context.Context, w io.Writer) context.Context {
return slog.Make(ctx, &humanSink{
w: syncwriter.New(w),
w2: w,
Expand Down
8 changes: 4 additions & 4 deletions sloggers/sloghuman/sloghuman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"context"
"testing"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
"cdr.dev/slog/v2/internal/entryhuman"
"cdr.dev/slog/v2/sloggers/sloghuman"
)

func TestMake(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions sloggers/slogjson/slogjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
// "logger_names": ["comp", "subcomp"],
// "msg": "hi",
// "caller": "slog/examples_test.go:62",
// "func": "cdr.dev/slog/sloggers/slogtest_test.TestExampleTest",
// "func": "cdr.dev/slog/v2/sloggers/slogtest_test.TestExampleTest",
// "trace": "<traceid>",
// "span": "<spanid>",
// "fields": {
// "my_field": "field value"
// }
// }
package slogjson // import "cdr.dev/slog/sloggers/slogjson"
package slogjson // import "cdr.dev/slog/v2/sloggers/slogjson"

import (
"context"
Expand All @@ -25,8 +25,8 @@ import (

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/syncwriter"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/syncwriter"
)

// Make creates a logger that writes JSON logs
Expand Down
10 changes: 5 additions & 5 deletions sloggers/slogjson/slogjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
"cdr.dev/slog/internal/entryjson"
"cdr.dev/slog/sloggers/slogjson"
"cdr.dev/slog/v2"
"cdr.dev/slog/v2/internal/assert"
"cdr.dev/slog/v2/internal/entryjson"
"cdr.dev/slog/v2/sloggers/slogjson"
)

var _, slogjsonTestFile, _, _ = runtime.Caller(0)
Expand All @@ -29,7 +29,7 @@ func TestMake(t *testing.T) {
slog.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))

j := entryjson.Filter(b.String(), "ts")
exp := fmt.Sprintf(`{"level":"ERROR","msg":"line1\n\nline2","caller":"%v:29","func":"cdr.dev/slog/sloggers/slogjson_test.TestMake","logger_names":["named"],"trace":"%v","span":"%v","fields":{"wowow":"me\nyou"}}
exp := fmt.Sprintf(`{"level":"ERROR","msg":"line1\n\nline2","caller":"%v:29","func":"cdr.dev/slog/v2/sloggers/slogjson_test.TestMake","logger_names":["named"],"trace":"%v","span":"%v","fields":{"wowow":"me\nyou"}}
`, slogjsonTestFile, s.SpanContext().TraceID, s.SpanContext().SpanID)
assert.Equal(t, "entry", exp, j)
}
2 changes: 1 addition & 1 deletion sloggers/slogstackdriver/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package slogstackdriver
import (
logpbtype "google.golang.org/genproto/googleapis/logging/type"

"cdr.dev/slog"
"cdr.dev/slog/v2"
)

func Sev(level slog.Level) logpbtype.LogSeverity {
Expand Down
Loading