From 888d00e2e9f78344dea435af049a9768113cdc3c Mon Sep 17 00:00:00 2001 From: Noel Cower Date: Tue, 11 Feb 2020 14:01:45 -0800 Subject: [PATCH] Fix README example Move it into example_test.go so that it can be checked as well. --- README.md | 5 +++-- example_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 example_test.go diff --git a/README.md b/README.md index 7440c82..b05d05e 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ A few examples of basic usage follow. ```go // Create a logger at info level with a production configuration. -l, err := clog.New(zap.InfoLevel, false) +level := zap.NewAtomicLevelAt(zap.InfoLevel) +l, err := clog.New(level, false) if err != nil { - ... + panic(err) } l.Info("Ready") ``` diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..3b281df --- /dev/null +++ b/example_test.go @@ -0,0 +1,27 @@ +package clog_test + +import ( + "context" + + "github.com/Kochava/clog" + "go.uber.org/zap" +) + +func ExampleNew() { + // Create a logger at info level with a production configuration. + level := zap.NewAtomicLevelAt(zap.InfoLevel) + l, err := clog.New(level, false) + if err != nil { + panic(err) + } + l.Info("Ready") + + // Attach the logger, l, to a context: + ctx := clog.WithLogger(context.Background(), l) + + // Attach fields to the logger: + ctx = clog.With(ctx, zap.Int("field", 1234)) + + // Log at info level: + clog.Info(ctx, "Log message") +} -- 2.45.2