-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Package lagerctx lets Lager Loggers be put inside the standard library Context type. closes #25
- Loading branch information
Christopher Brown
committed
Jun 12, 2017
1 parent
b4f07fd
commit dfcbcba
Showing
5 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Package lagerctx provides convenience when using Lager with the context | ||
// feature of the standard library. | ||
package lagerctx | ||
|
||
import ( | ||
"context" | ||
|
||
"code.cloudfoundry.org/lager" | ||
) | ||
|
||
// NewContext returns a derived context containing the logger. | ||
func NewContext(parent context.Context, logger lager.Logger) context.Context { | ||
return context.WithValue(parent, contextKey{}, logger) | ||
} | ||
|
||
// FromContext returns the logger contained in the context, or an inert logger | ||
// that will not log anything. | ||
func FromContext(ctx context.Context) lager.Logger { | ||
l, ok := ctx.Value(contextKey{}).(lager.Logger) | ||
if !ok { | ||
return &discardLogger{} | ||
} | ||
|
||
return l | ||
} | ||
|
||
// WithSession returns a new logger that has, for convenience, had a new | ||
// session created on it. | ||
func WithSession(ctx context.Context, task string, data ...lager.Data) lager.Logger { | ||
return FromContext(ctx).Session(task, data...) | ||
} | ||
|
||
// WithData returns a new logger that has, for convenience, had new data added | ||
// to on it. | ||
func WithData(ctx context.Context, data lager.Data) lager.Logger { | ||
return FromContext(ctx).WithData(data) | ||
} | ||
|
||
// contextKey is used to retrieve the logger from the context. | ||
type contextKey struct{} | ||
|
||
// discardLogger is an inert logger. | ||
type discardLogger struct{} | ||
|
||
func (*discardLogger) Debug(string, ...lager.Data) {} | ||
func (*discardLogger) Info(string, ...lager.Data) {} | ||
func (*discardLogger) Error(string, error, ...lager.Data) {} | ||
func (*discardLogger) Fatal(string, error, ...lager.Data) {} | ||
func (*discardLogger) RegisterSink(lager.Sink) {} | ||
func (*discardLogger) SessionName() string { return "" } | ||
func (d *discardLogger) Session(string, ...lager.Data) lager.Logger { return d } | ||
func (d *discardLogger) WithData(lager.Data) lager.Logger { return d } |
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,55 @@ | ||
package lagerctx_test | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/gbytes" | ||
|
||
"code.cloudfoundry.org/lager" | ||
"code.cloudfoundry.org/lager/lagerctx" | ||
"code.cloudfoundry.org/lager/lagertest" | ||
) | ||
|
||
var _ = Describe("Lager Context", func() { | ||
It("can store loggers inside contexts", func() { | ||
l := lagertest.NewTestLogger("lagerctx") | ||
ctx := lagerctx.NewContext(context.Background(), l) | ||
|
||
logger := lagerctx.FromContext(ctx) | ||
logger.Info("from-a-context") | ||
|
||
Expect(l.LogMessages()).To(HaveLen(1)) | ||
}) | ||
|
||
It("can add a session to the logger in the context", func() { | ||
l := lagertest.NewTestLogger("lagerctx") | ||
ctx := lagerctx.NewContext(context.Background(), l) | ||
|
||
logger := lagerctx.WithSession(ctx, "new-session", lager.Data{ | ||
"bespoke-data": "", | ||
}) | ||
logger.Info("from-a-context") | ||
|
||
Expect(l).To(gbytes.Say("new-session")) | ||
Expect(l).To(gbytes.Say("bespoke-data")) | ||
}) | ||
|
||
It("can add data to the logger in the context", func() { | ||
l := lagertest.NewTestLogger("lagerctx") | ||
ctx := lagerctx.NewContext(context.Background(), l) | ||
|
||
logger := lagerctx.WithData(ctx, lager.Data{ | ||
"bespoke-data": "", | ||
}) | ||
logger.Info("from-a-context") | ||
|
||
Expect(l).To(gbytes.Say("bespoke-data")) | ||
}) | ||
|
||
It("will be fine if there is no logger in the context", func() { | ||
logger := lagerctx.FromContext(context.Background()) | ||
logger.Info("from-a-context") | ||
}) | ||
}) |
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,13 @@ | ||
package lagerctx_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestLagerctx(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Lagerctx Suite") | ||
} |
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 @@ | ||
package lagerctx // import "code.cloudfoundry.org/lager/lagerctx" |
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