Skip to content

Commit

Permalink
Log API Refactor (#22)
Browse files Browse the repository at this point in the history
* removed WithFunc, fixes #19

* renamed WithCtxRef to WithContextRef

* changed to WithContextKey
  • Loading branch information
nofun97 authored Feb 15, 2020
1 parent f3788aa commit 670f234
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 49 deletions.
20 changes: 5 additions & 15 deletions log/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions log/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
18 changes: 3 additions & 15 deletions log/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
20 changes: 3 additions & 17 deletions log/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 670f234

Please sign in to comment.