Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Add convenience methods Key() and Value() to log.Field #116

Merged
merged 1 commit into from
Sep 27, 2016

Conversation

RaduBerinde
Copy link
Contributor

@RaduBerinde RaduBerinde commented Sep 27, 2016

Currently the only way to print a Field is to define an Encoder. This is a
lot of code to write, especially if it's needed just for debugging or a quick
test. Adding a Value() method that returns the value as an interface{}
(which can be passed to fmt.Print functions), as well as a String() method.

Also exporting the key field.

CC @tschottdorf

// method.
//
// "heavily influenced by" (i.e., partially stolen from)
// https://github.com/uber-go/zap
type Field struct {
key string
Key string
Copy link
Member

Choose a reason for hiding this comment

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

would it make more sense to expose Key() instead? That would keep the Field immutable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Currently the only way to print a `Field` is to define an `Encoder`. This is a
lot of code to write, especially if it's needed just for debugging or a quick
test. Adding a `Value()` method that returns the value as an `interface{}`
(which can be passed to `fmt.Print` functions), as well as a `String()` method.

Also exporting the key field.
Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

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

lgtm, let's wait for @bensigelman

Copy link
Contributor

@bhs bhs left a comment

Choose a reason for hiding this comment

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

I was being a purist and implementing custom Encoders in various places... what you have here is more sensible. :)

return math.Float32frombits(uint32(lf.numericVal))
case float64Type:
return math.Float64frombits(uint64(lf.numericVal))
case errorType, objectType, lazyLoggerType:
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder what we should do for lazyLoggerType... I guess this is "fine for now", though I can imagine someone expecting to see the thing the lazy log function encodes instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah.. Yeah I guess we would need to define an Encoder that captures the emitted key-value, and run the lazy logger. I'll file an issue.

BTW this makes me curious - why does the Encoder interface have an EmitLazyLogger method? Wouldn't any implementation of that (that follows the intended purpose) always just call the lazyLogger? Seems that instead of calling visitor.EmitLazyLogger(lf.interfaceVal.(LazyLogger)) we could call lf.interfaceVal.(LazyLogger)(visitor) no?

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC this was adapted somewhat blindly from the zap approach... my only rationale would be that some impls might not want to do lazy logging at all, and the way things are structured now gives them more control (???). But I am just rationalizing.

Copy link
Member

Choose a reason for hiding this comment

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

@RaduBerinde I think the Encoder serves two purposes. At the top level it allows the implementations to be told that a log field has a lazy encoder, so the respective method is needed. But then the same interface is reused recursively in the lazy encoder signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, thanks. There are a few more things that could use some clarification, I filed #118 so we can move the discussion there.

@@ -20,7 +23,7 @@ const (
)

// Field instances are constructed via LogBool, LogString, and so on.
// Tracing implementations may then handle them via the Field.Process
// Tracing implementations may then handle them via the Field.Marshal
Copy link
Contributor

Choose a reason for hiding this comment

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

(thanks)

@bhs bhs merged commit 30dda93 into opentracing:master Sep 27, 2016
@RaduBerinde RaduBerinde deleted the field-stuff branch September 27, 2016 16:44
@yurishkuro yurishkuro changed the title Add convenience methods to log.Field Add convenience methods Key() and Value() to log.Field Sep 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants