From 670f2343ba9c8d11d1dc7ecda1df26d574558321 Mon Sep 17 00:00:00 2001 From: Novan Allanadi <25704935+nofun97@users.noreply.github.com> Date: Sat, 15 Feb 2020 12:30:49 +1100 Subject: [PATCH] Log API Refactor (#22) * removed WithFunc, fixes #19 * renamed WithCtxRef to WithContextRef * changed to WithContextKey --- log/api.go | 20 +++++--------------- log/api_test.go | 4 ++-- log/examples.md | 18 +++--------------- log/examples/example.go | 20 +++----------------- 4 files changed, 13 insertions(+), 49 deletions(-) diff --git a/log/api.go b/log/api.go index 3c3035b..e09b4c8 100644 --- a/log/api.go +++ b/log/api.go @@ -50,16 +50,11 @@ func WithConfigs(configs ...Config) Fields { return Fields{}.WithConfigs(configs...) } -// WithCtxRef creates a field with a key that refers to the provided context key, +// WithContextKey creates a field with a key that refers to the provided context key, // fields will use key as the fields property and take the value that corresponds // to ctxKey. -func WithCtxRef(key string, ctxKey interface{}) Fields { - return Fields{}.WithCtxRef(key, ctxKey) -} - -// WithFunc creates a field with a string key and a callback value. -func WithFunc(key string, f func(context.Context) interface{}) Fields { - return Fields{}.WithFunc(key, f) +func WithContextKey(key string, ctxKey interface{}) Fields { + return Fields{}.WithContextKey(key, ctxKey) } // WithLogger adds logger which will be used for the log operation. @@ -135,16 +130,11 @@ func (f Fields) WithConfigs(configs ...Config) Fields { }) } -// WithCtxRef adds key and the context key to the fields. -func (f Fields) WithCtxRef(key string, ctxKey interface{}) Fields { +// WithContextKey adds key and the context key to the fields. +func (f Fields) WithContextKey(key string, ctxKey interface{}) Fields { return f.with(key, ctxRef{ctxKey}) } -// WithFunc adds key and the function to the fields. -func (f Fields) WithFunc(key string, val func(context.Context) interface{}) Fields { - return f.with(key, val) -} - // WithLogger adds logger which will be used for the log operation. func (f Fields) WithLogger(logger Logger) Fields { return f.with(loggerKey{}, logger.(copyable).Copy()) diff --git a/log/api_test.go b/log/api_test.go index d0f01f7..4332fe2 100644 --- a/log/api_test.go +++ b/log/api_test.go @@ -221,10 +221,10 @@ func TestWith(t *testing.T) { ) } -func TestWithCtxRef(t *testing.T) { +func TestWithContextRef(t *testing.T) { t.Parallel() - f := WithCtxRef("key1", key1{}).WithCtxRef("key2", key2{}).WithCtxRef("key3", key3{}) + f := WithContextKey("key1", key1{}).WithContextKey("key2", key2{}).WithContextKey("key3", key3{}) for i := f.m.Range(); i.Next(); { assert.IsType(t, ctxRef{}, i.Value()) diff --git a/log/examples.md b/log/examples.md index be1734e..25210ed 100644 --- a/log/examples.md +++ b/log/examples.md @@ -43,21 +43,12 @@ func fieldsDemo(ctx context.Context) { // With adds a regular key value pair. fields := log.With("hello", "world") - // WithCtxRef adds a key whose value will be taken from the context + // WithContextRef adds a key whose value will be taken from the context // before logging. You have to define an alias to the key which will // be used during logging as context key are usually a struct or iota // which has no information about it when logged. If the key does not // exist in the context, it will not be logged. - fields = fields.WithCtxRef("my alias", contextKey{}) - - // WithFunc adds a key and a function with a context argument which - // will be called before logging. If the result of the function is - // nil, it will not be logged. - ctx = context.WithValue(ctx, "bar", 42) - fields = fields.WithFunc("foo", func(ctx context.Context) interface{} { - return ctx.Value("bar") - }) - ctx = context.WithValue(ctx, contextKey{}, "now exist in context") + fields = fields.WithContextRef("my alias", contextKey{}) // Fields operation can also be chained either by the With APIs or Chain API. // An important thing to note is that fields operation always merge with the @@ -68,10 +59,7 @@ func fieldsDemo(ctx context.Context) { // In this example, the final fields will have ("test": "test four") instead of ("test": "test too"). fields = fields. With("test", "test too"). - WithCtxRef("test three", contextKey2{}). - WithFunc("doesn't", func(context.Context) interface{} { - return "matter" - }). + WithContextRef("test three", contextKey2{}). With("test", "test four") // The final fields will have ("out of": "things to write") diff --git a/log/examples/example.go b/log/examples/example.go index 343e146..22d8f3a 100644 --- a/log/examples/example.go +++ b/log/examples/example.go @@ -25,23 +25,12 @@ func fieldsDemo(ctx context.Context) { // With adds a regular key value pair. fields := log.With("hello", "world") - // WithCtxRef adds a key whose value will be taken from the context + // WithContextKey adds a key whose value will be taken from the context // before logging. You have to define an alias to the key which will // be used during logging as context key are usually a struct or iota // which has no information about it when logged. If the key does not // exist in the context, it will not be logged. - fields = fields.WithCtxRef("my alias", contextKey{}) - - // WithFunc adds a key and a function with a context argument which - // will be called before logging. If the result of the function is - // nil, it will not be logged. - ctx = context.WithValue(ctx, "bar", 42) - fields = fields.WithFunc("foo", func(ctx context.Context) interface{} { - return ctx.Value("bar") - }) - fmt.Printf("Current Fields after using the With API: %s\n", fields.String(ctx)) - ctx = context.WithValue(ctx, contextKey{}, "now exist in context") - fmt.Printf("Current Fields after using the With API and adding my alias to context: %s\n", fields.String(ctx)) + fields = fields.WithContextKey("my alias", contextKey{}) // Fields operation can also be chained either by the With APIs or Chain API. // An important thing to note is that fields operation always merge with the @@ -52,10 +41,7 @@ func fieldsDemo(ctx context.Context) { // In this example, the final fields will have ("test": "test four") instead of ("test": "test too"). fields = fields. With("test", "test too"). - WithCtxRef("test three", contextKey2{}). - WithFunc("doesn't", func(context.Context) interface{} { - return "matter" - }). + WithContextKey("test three", contextKey2{}). With("test", "test four") // The final fields will have ("out of": "things to write")